mirror of
https://github.com/ClassiCube/MCGalaxy.git
synced 2025-09-19 04:07:10 -04:00
Discord: Properly handle DMs (no need for .x, don't send to in-game chat)
This commit is contained in:
parent
67d6e6e2d1
commit
ff11c110f4
@ -31,6 +31,7 @@ namespace MCGalaxy.Modules.Relay.Discord {
|
||||
DiscordApiClient api;
|
||||
DiscordWebsocket socket;
|
||||
string botUserID;
|
||||
Dictionary<string, bool> isDMChannel = new Dictionary<string, bool>();
|
||||
|
||||
public override string RelayName { get { return "Discord"; } }
|
||||
public override bool Enabled { get { return Config.Enabled; } }
|
||||
@ -73,9 +74,12 @@ namespace MCGalaxy.Modules.Relay.Discord {
|
||||
OpChannels = Config.OpChannels.SplitComma();
|
||||
|
||||
socket.Token = Config.BotToken;
|
||||
socket.Handler = HandleEvent;
|
||||
socket.GetStatus = GetStatus;
|
||||
|
||||
socket.OnReady = HandleReadyEvent;
|
||||
socket.OnMessageCreate = HandleMessageEvent;
|
||||
socket.OnChannelCreate = HandleChannelEvent;
|
||||
|
||||
Thread worker = new Thread(IOThread);
|
||||
worker.Name = "DiscordRelayBot";
|
||||
worker.IsBackground = true;
|
||||
@ -94,27 +98,6 @@ namespace MCGalaxy.Modules.Relay.Discord {
|
||||
}
|
||||
|
||||
|
||||
void HandleEvent(JsonObject obj) {
|
||||
// actually handle the event
|
||||
string eventName = (string)obj["t"];
|
||||
|
||||
if (eventName == "READY") HandleReadyEvent(obj);
|
||||
if (eventName == "MESSAGE_CREATE") HandleMessageEvent(obj);
|
||||
}
|
||||
|
||||
|
||||
void HandleReadyEvent(JsonObject obj) {
|
||||
JsonObject data = (JsonObject)obj["d"];
|
||||
JsonObject user = (JsonObject)data["user"];
|
||||
botUserID = (string)user["id"];
|
||||
|
||||
api = new DiscordApiClient();
|
||||
api.Token = Config.BotToken;
|
||||
|
||||
api.RunAsync();
|
||||
RegisterEvents();
|
||||
}
|
||||
|
||||
string GetNick(JsonObject data) {
|
||||
if (!Config.UseNicks) return null;
|
||||
object raw;
|
||||
@ -138,6 +121,18 @@ namespace MCGalaxy.Modules.Relay.Discord {
|
||||
user.ID = (string)author["id"];
|
||||
return user;
|
||||
}
|
||||
|
||||
|
||||
void HandleReadyEvent(JsonObject data) {
|
||||
JsonObject user = (JsonObject)data["user"];
|
||||
botUserID = (string)user["id"];
|
||||
|
||||
api = new DiscordApiClient();
|
||||
api.Token = Config.BotToken;
|
||||
|
||||
api.RunAsync();
|
||||
RegisterEvents();
|
||||
}
|
||||
|
||||
void PrintAttachments(JsonObject data, string channel) {
|
||||
object raw;
|
||||
@ -156,18 +151,28 @@ namespace MCGalaxy.Modules.Relay.Discord {
|
||||
}
|
||||
}
|
||||
|
||||
void HandleMessageEvent(JsonObject obj) {
|
||||
JsonObject data = (JsonObject)obj["d"];
|
||||
RelayUser user = ExtractUser(data);
|
||||
void HandleMessageEvent(JsonObject data) {
|
||||
RelayUser user = ExtractUser(data);
|
||||
// ignore messages from self
|
||||
if (user.ID == botUserID) return;
|
||||
|
||||
string channel = (string)data["channel_id"];
|
||||
string message = (string)data["content"];
|
||||
message = ParseMessage(message);
|
||||
bool isDM;
|
||||
|
||||
HandleChannelMessage(user, channel, message);
|
||||
PrintAttachments(data, channel);
|
||||
if (isDMChannel.TryGetValue(channel, out isDM)) {
|
||||
HandleDirectMessage(user, channel, message);
|
||||
} else {
|
||||
HandleChannelMessage(user, channel, message);
|
||||
PrintAttachments(data, channel);
|
||||
}
|
||||
}
|
||||
|
||||
void HandleChannelEvent(JsonObject data) {
|
||||
string channel = (string)data["id"];
|
||||
string type = (string)data["type"];
|
||||
|
||||
if (type == "1") isDMChannel[channel] = true;
|
||||
}
|
||||
|
||||
|
||||
|
@ -33,12 +33,17 @@ namespace MCGalaxy.Modules.Relay.Discord {
|
||||
|
||||
/// <summary> Authorisation token for the bot account </summary>
|
||||
public string Token;
|
||||
/// <summary> Delegate invoked when a message has been received </summary>
|
||||
public Action<JsonObject> Handler;
|
||||
/// <summary> Callback function to retrieve the activity status message </summary>
|
||||
public Func<string> GetStatus;
|
||||
public bool CanReconnect = true;
|
||||
|
||||
/// <summary> Callback invoked when a ready event has been received </summary>
|
||||
public Action<JsonObject> OnReady;
|
||||
/// <summary> Callback invoked when a message created event has been received </summary>
|
||||
public Action<JsonObject> OnMessageCreate;
|
||||
/// <summary> Callback invoked when a channel created event has been received </summary>
|
||||
public Action<JsonObject> OnChannelCreate;
|
||||
|
||||
readonly object sendLock = new object();
|
||||
SchedulerTask heartbeat;
|
||||
string lastSequence;
|
||||
@ -92,10 +97,10 @@ namespace MCGalaxy.Modules.Relay.Discord {
|
||||
const int REASON_INVALID_TOKEN = 4004;
|
||||
|
||||
protected override void Disconnect(int reason) {
|
||||
if (reason == REASON_INVALID_TOKEN) {
|
||||
Logger.Log(LogType.Warning, "Discord relay: Invalid bot token provided - unable to connect");
|
||||
CanReconnect = false;
|
||||
}
|
||||
if (reason == REASON_INVALID_TOKEN) {
|
||||
Logger.Log(LogType.Warning, "Discord relay: Invalid bot token provided - unable to connect");
|
||||
CanReconnect = false;
|
||||
}
|
||||
Logger.Log(LogType.SystemActivity, "Discord relay bot closing: " + reason);
|
||||
|
||||
try {
|
||||
@ -151,7 +156,19 @@ namespace MCGalaxy.Modules.Relay.Discord {
|
||||
if (obj.TryGetValue("s", out sequence))
|
||||
lastSequence = (string)sequence;
|
||||
|
||||
Handler(obj);
|
||||
string eventName = (string)obj["t"];
|
||||
JsonObject data;
|
||||
|
||||
if (eventName == "READY") {
|
||||
data = (JsonObject)obj["d"];
|
||||
OnReady(data);
|
||||
} else if (eventName == "MESSAGE_CREATE") {
|
||||
data = (JsonObject)obj["d"];
|
||||
OnMessageCreate(data);
|
||||
} else if (eventName == "CHANNEL_CREATE") {
|
||||
data = (JsonObject)obj["d"];
|
||||
OnChannelCreate(data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -258,7 +258,7 @@ namespace MCGalaxy {
|
||||
RelayUser rUser = new RelayUser();
|
||||
rUser.ID = user.Nick;
|
||||
rUser.Nick = user.Nick;
|
||||
HandleUserMessage(rUser, user.Nick, message);
|
||||
HandleDirectMessage(rUser, user.Nick, message);
|
||||
}
|
||||
|
||||
void OnPublic(UserInfo user, string channel, string message) {
|
||||
|
@ -46,8 +46,8 @@ namespace MCGalaxy.Modules.Relay {
|
||||
protected byte retries;
|
||||
|
||||
|
||||
/// <summary> The name of service this relay bot communicates with </summary>
|
||||
/// <remarks> IRC, Discord, etc </remarks>
|
||||
/// <summary> The name of the service this relay bot communicates with </summary>
|
||||
/// <example> IRC, Discord </example>
|
||||
public abstract string RelayName { get; }
|
||||
|
||||
/// <summary> Whether this relay bot is currently enabled </summary>
|
||||
@ -226,7 +226,7 @@ namespace MCGalaxy.Modules.Relay {
|
||||
}
|
||||
|
||||
/// <summary> Handles a direct message written by the given user </summary>
|
||||
protected void HandleUserMessage(RelayUser user, string channel, string message) {
|
||||
protected void HandleDirectMessage(RelayUser user, string channel, string message) {
|
||||
message = ParseMessage(message);
|
||||
string[] parts = message.SplitSpaces(2);
|
||||
string cmdName = parts[0].ToLower();
|
||||
|
@ -251,7 +251,6 @@ namespace Sharkbite.Irc
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//Trap a connection failure
|
||||
listener.Error( ReplyCode.ConnectionFailed, "Unhandled error: " + ex);
|
||||
}
|
||||
finally
|
||||
|
Loading…
x
Reference in New Issue
Block a user