Skip to content
This repository has been archived by the owner on Sep 1, 2021. It is now read-only.

Commit

Permalink
Merge pull request #35 from ldilley/master
Browse files Browse the repository at this point in the history
Convert received Discord mentions and change uptime command to embed
  • Loading branch information
ldilley authored Jan 9, 2020
2 parents b71a957 + 5f397a6 commit 03fef7d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 17 deletions.
67 changes: 55 additions & 12 deletions Terracord/Discord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using Discord;
using Discord.WebSocket;
using System;
using System.Text;
using System.Threading.Tasks;
using TShockAPI;

Expand Down Expand Up @@ -166,14 +167,17 @@ private Task MessageReceived(SocketMessage message)
if(message.Author.Id == Client.CurrentUser.Id)
return Task.CompletedTask;

// Check for a command
// Check for and handle commands
if(message.Content.StartsWith(Config.CommandPrefix.ToString()) && message.Content.Length > 1)
CommandHandler(message.Content);

// Check for mentions and convert them to friendly names if found
string messageContent = ConvertMentions(message);

// Relay Discord message to Terraria players
if(Config.LogChat)
Util.Log($"<{message.Author.Username}@Discord> {message.Content}", Util.Severity.Info);
TShock.Utils.Broadcast($"<{message.Author.Username}@Discord> {message.Content}", Config.BroadcastColor[0], Config.BroadcastColor[1], Config.BroadcastColor[2]);
Util.Log($"<{message.Author.Username}@Discord> {messageContent}", Util.Severity.Info);
TShock.Utils.Broadcast($"<{message.Author.Username}@Discord> {messageContent}", Config.BroadcastColor[0], Config.BroadcastColor[1], Config.BroadcastColor[2]);
}
catch(Exception e)
{
Expand All @@ -183,6 +187,23 @@ private Task MessageReceived(SocketMessage message)
return Task.CompletedTask;
}

/// <summary>
/// Sends a message to a Discord channel
/// </summary>
/// <param name="message">message to send to Discord channel</param>
public void Send(string message)
{
try
{
// channel? checks if object is null prior to sending message
channel?.SendMessageAsync(message);
}
catch(Exception e)
{
Util.Log($"Unable to send Discord message: {e.Message}", Util.Severity.Error);
}
}

/// <summary>
/// Handles Discord commands
/// </summary>
Expand Down Expand Up @@ -218,26 +239,48 @@ private Task CommandHandler(string command)
}

if(command.Equals("uptime", StringComparison.OrdinalIgnoreCase))
Send($"**__Uptime__**\n```\n{Util.Uptime()}\n```");
{
//Send($"**__Uptime__**\n```\n{Util.Uptime()}\n```");
EmbedBuilder embed = new EmbedBuilder();
embed.WithColor(Color.Blue)
.WithDescription(Util.Uptime())
.WithFooter(footer => footer.Text = $"Terracord {Terracord.version}")
.WithCurrentTimestamp()
.WithTitle("Uptime");
channel.SendMessageAsync("", false, embed.Build());
}

return Task.CompletedTask;
}

/// <summary>
/// Sends a message to a Discord channel
/// Converts channel, role, and user mentions to friendly names before being broadcasted to TShock players
/// </summary>
/// <param name="message">message to send to Discord channel</param>
public void Send(string message)
/// <param name="message">message received by Discord bot</param>
/// <returns>modified message text</returns>
private string ConvertMentions(SocketMessage message)
{
try
StringBuilder modifiedMessageText = new StringBuilder(message.Content);

if(message.MentionedChannels.Count > 0)
{
// channel? checks if object is null prior to sending message
channel?.SendMessageAsync(message);
foreach(var channel in message.MentionedChannels)
modifiedMessageText = modifiedMessageText.Replace($"<#{channel.Id}>", $"#{channel.Name}");
}
catch(Exception e)

if(message.MentionedRoles.Count > 0)
{
Util.Log($"Unable to send Discord message: {e.Message}", Util.Severity.Error);
foreach(var role in message.MentionedRoles)
modifiedMessageText = modifiedMessageText.Replace($"<@&{role.Id}>", $"@{role.Name}");
}

if(message.MentionedUsers.Count > 0)
{
foreach(var user in message.MentionedUsers)
modifiedMessageText = modifiedMessageText.Replace($"<@!{user.Id}>", $"@{user.Username}");
}

return modifiedMessageText.ToString();
}
}
}
13 changes: 8 additions & 5 deletions Terracord/Terracord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,17 @@ private void OnChat(ServerChatEventArgs args)
* 1. Check args.Text for regex match of a tag.
* 2. If 1 or more instances found, iterate through Discord server members to find potential username matches.
* 3. Replace @user tags in args.Text with user.Mention and send the message
var guilds = botClient.Guilds;
var guilds = discord.Client.Guilds;
foreach(var guild in guilds)
{
var members = guild.Users;
foreach(var member in members)
foreach(var role in guild.Roles)
Console.WriteLine(role.Mention);
foreach(var channel in guild.TextChannels)
Console.WriteLine(channel.Mention);
foreach(var user in guild.Users)
{
if("test".Equals(member.Username, StringComparison.OrdinalIgnoreCase))
Console.WriteLine($"{member.Mention}");
if("test".Equals(user.Username, StringComparison.OrdinalIgnoreCase))
Console.WriteLine($"{user.Mention}");
}
}
*/
Expand Down

0 comments on commit 03fef7d

Please sign in to comment.