-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiscordWrapper_Events.cs
155 lines (138 loc) · 5.47 KB
/
DiscordWrapper_Events.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using System.Threading.Tasks;
using AwesomeChatBot.Discord.Objects;
using Discord;
using Discord.WebSocket;
using Microsoft.Extensions.Logging;
namespace AwesomeChatBot.Discord
{
public partial class DiscordWrapper
{
/// <summary>
/// Raised when a new server becomes available (connected)
/// </summary>
/// <param name="server"></param>
protected Task OnServerAvailableAsync(SocketGuild server)
{
return Task.Run(async () =>
{
// Create the server object
var serverObj = new DiscordGuild(this, server);
// Propagate event to framework
await OnServerAvailableAsync(serverObj).ConfigureAwait(false);
});
}
/// <summary>
/// When a new message is received
/// </summary>
/// <param name="message"></param>
protected Task OnMessageReceivedAsync(SocketMessage message)
{
return Task.Run(async () =>
{
var botUserMention = DiscordClient.CurrentUser.Mention.Replace("!", "");
var isMentioned = message.Content.Contains(botUserMention)
|| message.Content.Contains(DiscordClient.CurrentUser.Mention);
// Create the message object
var messageObj = new DiscordReceivedMessage(this, message, isMentioned);
// Propagate the event
await OnMessageReceivedAsync(messageObj).ConfigureAwait(false);
});
}
/// <summary>
/// When a new message is deleted
/// </summary>
/// <param name="cacheable"></param>
/// <param name="channel"></param>
protected Task OnMessageDeletedAsync(Cacheable<IMessage, ulong> cacheable, Cacheable<IMessageChannel, ulong> channel)
{
return Task.Run(async () =>
{
if (cacheable.Value is SocketMessage socketMessage)
{
var botUserMention = DiscordClient.CurrentUser.Mention.Replace("!", "");
var isMentioned = socketMessage.Content.Contains(botUserMention)
|| socketMessage.Content.Contains(DiscordClient.CurrentUser.Mention);
// Create the message object
var messageObj = new DiscordReceivedMessage(this, socketMessage, isMentioned);
// Propagate the event
await OnMessageDeletedAsync(messageObj).ConfigureAwait(false);
}
});
}
/// <summary>
/// When a server becomes unavailable (disconnected)
/// </summary>
/// <param name="server"></param>
protected Task OnServerUnavailableAsync(SocketGuild server)
{
return Task.Run(async () =>
{
// Create the server object
var serverObj = new DiscordGuild(this, server);
// Raise the event
await OnServerUnavailableAsync(serverObj).ConfigureAwait(false);
});
}
/// <summary>
/// When a new user joins on a server
/// </summary>
/// <param name="user"></param>
protected Task OnUserJoinedAsync(SocketGuildUser user)
{
return Task.Run(async () =>
{
// Materialize the objects
var userObj = new DiscordUser(this, user);
var serverObj = new DiscordGuild(this, user.Guild);
await OnNewUserJoinedServerAsync(userObj, serverObj).ConfigureAwait(false);
});
}
/// <summary>
/// When the bot joins a new server
/// </summary>
/// <param name="server">The server that was joined</param>
protected Task OnJoinedNewServerAsync(SocketGuild server)
{
return Task.Run(async () =>
{
// Materialize the objects
var serverObj = new DiscordGuild(this, server);
await OnJoinedNewServerAsync(serverObj).ConfigureAwait(false);
});
}
/// <summary>
/// When the wrapper has connected to the discord API
/// </summary>
protected Task OnWrapperConnectedAsync()
{
return OnConnectedAsync();
}
/// <summary>
/// When the wrapper has disconnected form the discord API
/// </summary>
/// <param name="ex">The exception that caused the disconnect</param>
protected Task OnDisconnectedAsync(Exception ex)
{
return Task.Run(async () =>
{
Logger.Log(LogLevel.Error, $"Lost connection to the discord API: {ex}");
await base.OnDisconnectedAsync().ConfigureAwait(false);
});
}
/// <summary>
/// When the wrapper receives a reaction added
/// </summary>
/// <param name="cachable"></param>
/// <param name="channel"></param>
/// <param name="reaction"></param>
protected Task OnReactionAddedAsync(Cacheable<IUserMessage, ulong> cachable, Cacheable<IMessageChannel, ulong> channel, SocketReaction reaction)
{
return Task.Run(async () =>
{
var reactionObj = new DiscordReaction(this, reaction);
await OnReactionAddedAsync(reactionObj).ConfigureAwait(false);
});
}
}
}