Skip to content

Commit

Permalink
Discord: Add option to use nicks instead of usernames
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed May 3, 2021
1 parent 1d8e765 commit fffe4ed
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
15 changes: 14 additions & 1 deletion GUI/PropertyWindow/PropertyWindow.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions GUI/PropertyWindow/PropertyWindow.Relay.cs
Expand Up @@ -104,6 +104,7 @@ public partial class PropertyWindow : Form {
dis_txtToken.Text = cfg.BotToken;
dis_txtChannel.Text = cfg.Channels;
dis_txtOpChannel.Text = cfg.OpChannels;
dis_chkNicks.Checked = cfg.UseNicks;
ToggleDiscordSettings(cfg.Enabled);
}

Expand All @@ -113,6 +114,7 @@ public partial class PropertyWindow : Form {
cfg.BotToken = dis_txtToken.Text;
cfg.Channels = dis_txtChannel.Text;
cfg.OpChannels = dis_txtOpChannel.Text;
cfg.UseNicks = dis_chkNicks.Checked;
}

void SaveDiscordProps() {
Expand All @@ -123,6 +125,7 @@ public partial class PropertyWindow : Form {
dis_txtToken.Enabled = enabled; dis_lblToken.Enabled = enabled;
dis_txtChannel.Enabled = enabled; dis_lblChannel.Enabled = enabled;
dis_txtOpChannel.Enabled = enabled; dis_lblOpChannel.Enabled = enabled;
dis_chkNicks.Enabled = enabled;
}

void dis_chkEnabled_CheckedChanged(object sender, EventArgs e) {
Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/Levels/Level.cs
Expand Up @@ -133,7 +133,7 @@ public sealed partial class Level : IDisposable {
// Wait up to 1 second for physics thread to finish
physThread.Join(1000);
} catch {
// No physics thread, or physics thread is still busy
// No physics thread at all
}

Dispose();
Expand Down
17 changes: 15 additions & 2 deletions MCGalaxy/Modules/Relay/Discord/DiscordBot.cs
Expand Up @@ -97,15 +97,28 @@ public sealed class DiscordBot : RelayBot {
if (eventName == "MESSAGE_CREATE") HandleMessageEvent(obj);
}

string GetNick(JsonObject data) {
if (!Config.UseNicks) return null;
object raw;
if (!data.TryGetValue("member", out raw)) return null;

// Make sure this is really a member object first
JsonObject member = raw as JsonObject;
if (member == null) return null;

member.TryGetValue("nick", out raw);
return raw as string;
}

void HandleMessageEvent(JsonObject obj) {
JsonObject data = (JsonObject)obj["d"];
JsonObject author = (JsonObject)data["author"];
string channel = (string)data["channel_id"];
string message = (string)data["content"];

RelayUser user = new RelayUser();
user.Nick = (string)author["username"];
user.UserID = (string)author["id"];
user.Nick = GetNick(data) ?? (string)author["username"];
user.UserID = (string)author["id"];
HandleChannelMessage(user, channel, message);
}

Expand Down
2 changes: 2 additions & 0 deletions MCGalaxy/Modules/Relay/Discord/DiscordPlugin.cs
Expand Up @@ -28,6 +28,8 @@ public sealed class DiscordConfig {
public string BotToken = "";
[ConfigString("status-message", null, "with {PLAYERS} players")]
public string Status = "with {PLAYERS} players";
[ConfigBool("use-nicknames", null, true)]
public bool UseNicks = true;

[ConfigString("channel-ids", null, "", true)]
public string Channels = "";
Expand Down

0 comments on commit fffe4ed

Please sign in to comment.