Skip to content

Commit 16da404

Browse files
committed
rest of the eventargs implemented
1 parent 567c4b8 commit 16da404

File tree

8 files changed

+133
-39
lines changed

8 files changed

+133
-39
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Discord;
2+
using Discord.WebSocket;
3+
using Volte.Discord;
4+
using Volte.Services;
5+
6+
namespace Volte.Data.Objects.EventArgs
7+
{
8+
public class JoinedGuildEventArgs
9+
{
10+
private DatabaseService _db = VolteBot.GetRequiredService<DatabaseService>();
11+
public IGuild Guild { get; }
12+
public DiscordServer Config { get; }
13+
public DiscordSocketClient Client { get; }
14+
15+
public JoinedGuildEventArgs(SocketGuild guild)
16+
{
17+
Guild = guild;
18+
Config = _db.GetConfig(guild);
19+
Client = VolteBot.Client;
20+
}
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Discord;
2+
using Discord.WebSocket;
3+
using Volte.Discord;
4+
using Volte.Services;
5+
6+
namespace Volte.Data.Objects.EventArgs
7+
{
8+
public class LeftGuildEventArgs
9+
{
10+
private DatabaseService _db = VolteBot.GetRequiredService<DatabaseService>();
11+
public IGuild Guild { get; }
12+
public DiscordServer Config { get; }
13+
public DiscordSocketClient Client { get; }
14+
15+
public LeftGuildEventArgs(SocketGuild guild)
16+
{
17+
Guild = guild;
18+
Config = _db.GetConfig(guild);
19+
Client = VolteBot.Client;
20+
}
21+
}
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Threading.Tasks.Dataflow;
2+
using Discord;
3+
4+
namespace Volte.Data.Objects.EventArgs
5+
{
6+
public class LogEventArgs
7+
{
8+
public string Message { get; }
9+
public string Source { get; }
10+
public LogSeverity Severity { get; }
11+
public (LogMessage Internal, global::Discord.LogMessage Discord) LogMessage { get; }
12+
13+
public LogEventArgs(global::Discord.LogMessage message)
14+
{
15+
Message = message.Message;
16+
Source = message.Source;
17+
Severity = message.Severity;
18+
LogMessage = (Objects.LogMessage.FromDiscordLogMessage(message), message);
19+
}
20+
}
21+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Discord;
2+
using Discord.WebSocket;
3+
using Volte.Discord;
4+
using Volte.Services;
5+
6+
namespace Volte.Data.Objects.EventArgs
7+
{
8+
public class ReactionAddedEventArgs
9+
{
10+
private DatabaseService _db = VolteBot.GetRequiredService<DatabaseService>();
11+
public IUserMessage Message { get; }
12+
public ISocketMessageChannel Channel { get; }
13+
public IGuild Guild { get; }
14+
public SocketReaction Reaction { get; }
15+
public DiscordServer Config { get; }
16+
public DiscordSocketClient Client { get; }
17+
18+
public ReactionAddedEventArgs(Cacheable<IUserMessage, ulong> message, ISocketMessageChannel channel,
19+
SocketReaction reaction)
20+
{
21+
Message = message.Value;
22+
Channel = channel;
23+
Reaction = reaction;
24+
Guild = (channel as ITextChannel)?.Guild;
25+
Config = _db.GetConfig(Guild);
26+
Client = VolteBot.Client;
27+
}
28+
}
29+
}

src/Discord/VolteHandler.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,11 @@ public async Task InitAsync()
6666
sw.Stop();
6767
await _logger.Log(LogSeverity.Info, LogSource.Volte,
6868
$"Loaded {loaded.Count} modules and {loaded.Sum(m => m.Commands.Count)} commands loaded in {sw.ElapsedMilliseconds}ms.");
69-
_client.Log += _logger.Log;
70-
_client.JoinedGuild += _guild.OnJoinAsync;
71-
_client.LeftGuild += _guild.OnLeaveAsync;
72-
_client.ReactionAdded += _verification.CheckReactionAsync;
69+
_client.Log += async (m) => await _logger.Log(new LogEventArgs(m));
70+
_client.JoinedGuild += async (guild) => await _guild.OnJoinAsync(new JoinedGuildEventArgs(guild));
71+
_client.LeftGuild += async (guild) => await _guild.OnLeaveAsync(new LeftGuildEventArgs(guild));
72+
_client.ReactionAdded += async (m, c, r) =>
73+
await _verification.CheckReactionAsync(new ReactionAddedEventArgs(m, c, r));
7374
_client.UserJoined += async (user) =>
7475
{
7576
var args = new UserJoinedEventArgs(user);
@@ -81,7 +82,7 @@ await _logger.Log(LogSeverity.Info, LogSource.Volte,
8182
_client.UserLeft += async (user) => await _defaultWelcome.LeaveAsync(new UserLeftEventArgs(user));
8283
_client.UserJoined += async (user) => await _autorole.ApplyRoleAsync(new UserJoinedEventArgs(user));
8384
_client.Ready += async () => await _event.OnReady(new ReadyEventArgs(_client));
84-
_client.MessageReceived += async s =>
85+
_client.MessageReceived += async (s) =>
8586
{
8687
if (!(s is IUserMessage msg)) return;
8788
if (msg.Author.IsBot) return;
@@ -91,17 +92,15 @@ await _logger.Log(LogSeverity.Info, LogSource.Volte,
9192
return;
9293
}
9394

94-
var args = new MessageReceivedEventArgs(s);
95-
96-
await _blacklist.CheckMessageAsync(args);
97-
await _antilink.CheckMessageAsync(args);
98-
await _pingchecks.CheckMessageAsync(args);
99-
await HandleMessageAsync(args);
95+
await HandleMessageAsync(new MessageReceivedEventArgs(s));
10096
};
10197
}
10298

10399
private async Task HandleMessageAsync(MessageReceivedEventArgs args)
104100
{
101+
await _blacklist.CheckMessageAsync(args);
102+
await _antilink.CheckMessageAsync(args);
103+
await _pingchecks.CheckMessageAsync(args);
105104
var prefixes = new[] {args.Config.CommandPrefix, $"<@{args.Context.Client.CurrentUser.Id}> "};
106105
if (CommandUtilities.HasAnyPrefix(args.Message.Content, prefixes, StringComparison.OrdinalIgnoreCase, out _,
107106
out var cmd))

src/Services/GuildService.cs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Discord.WebSocket;
77
using Volte.Data;
88
using Volte.Data.Objects;
9+
using Volte.Data.Objects.EventArgs;
910
using Volte.Discord;
1011
using Volte.Extensions;
1112

@@ -21,21 +22,21 @@ public GuildService(LoggingService loggingService)
2122
_logger = loggingService;
2223
}
2324

24-
public async Task OnJoinAsync(IGuild guild)
25+
public async Task OnJoinAsync(JoinedGuildEventArgs args)
2526
{
26-
if (Config.BlacklistedOwners.Contains(guild.OwnerId))
27+
if (Config.BlacklistedOwners.Contains(args.Guild.OwnerId))
2728
{
2829
await _logger.Log(LogSeverity.Warning, LogSource.Volte,
29-
$"Left guild \"{guild.Name}\" owned by blacklisted owner {await guild.GetOwnerAsync()}.");
30-
await guild.LeaveAsync();
30+
$"Left guild \"{args.Guild.Name}\" owned by blacklisted owner {await args.Guild.GetOwnerAsync()}.");
31+
await args.Guild.LeaveAsync();
3132
return;
3233
}
3334

34-
var owner = await guild.GetOwnerAsync();
35+
var owner = await args.Guild.GetOwnerAsync();
3536

3637
var embed = new EmbedBuilder()
3738
.WithTitle("Hey there!")
38-
.WithAuthor(await guild.GetOwnerAsync())
39+
.WithAuthor(await args.Guild.GetOwnerAsync())
3940
.WithColor(Config.SuccessColor)
4041
.WithDescription("Thanks for inviting me! Here's some basic instructions on how to set me up.")
4142
.AddField("Set your admin role", "$adminrole {roleName}", true)
@@ -51,7 +52,7 @@ await _logger.Log(LogSeverity.Warning, LogSource.Volte,
5152
}
5253
catch (HttpException ignored) when (ignored.DiscordCode.Equals(50007))
5354
{
54-
var c = (await guild.GetTextChannelsAsync()).FirstOrDefault();
55+
var c = (await args.Guild.GetTextChannelsAsync()).FirstOrDefault();
5556
if (c != null) await embed.SendToAsync(c);
5657
}
5758

@@ -68,15 +69,15 @@ await logger.Log(LogSeverity.Error, LogSource.Service,
6869
}
6970

7071
var channel = VolteBot.Client.GetGuild(joinLeave.GuildId).GetTextChannel(joinLeave.ChannelId);
71-
var users = (await guild.GetUsersAsync()).Where(u => !u.IsBot).ToList();
72-
var bots = (await guild.GetUsersAsync()).Where(u => u.IsBot).ToList();
72+
var users = (await args.Guild.GetUsersAsync()).Where(u => !u.IsBot).ToList();
73+
var bots = (await args.Guild.GetUsersAsync()).Where(u => u.IsBot).ToList();
7374

7475
var e = new EmbedBuilder()
7576
.WithAuthor(owner)
7677
.WithTitle("Joined Guild")
77-
.AddField("Name", guild.Name, true)
78-
.AddField("ID", guild.Id, true)
79-
.WithThumbnailUrl(guild.IconUrl)
78+
.AddField("Name", args.Guild.Name, true)
79+
.AddField("ID", args.Guild.Id, true)
80+
.WithThumbnailUrl(args.Guild.IconUrl)
8081
.WithCurrentTimestamp()
8182
.AddField("Users", users.Count, true)
8283
.AddField("Bots", bots.Count, true);
@@ -97,7 +98,7 @@ await logger.Log(LogSeverity.Error, LogSource.Service,
9798
}
9899
}
99100

100-
public async Task OnLeaveAsync(SocketGuild guild)
101+
public async Task OnLeaveAsync(LeftGuildEventArgs args)
101102
{
102103
if (Config.JoinLeaveLog.Enabled)
103104
{
@@ -115,11 +116,11 @@ await logger.Log(LogSeverity.Error, LogSource.Service,
115116
try
116117
{
117118
var e = new EmbedBuilder()
118-
.WithAuthor(guild.Owner)
119+
.WithAuthor(await args.Guild.GetOwnerAsync())
119120
.WithTitle("Left Guild")
120-
.AddField("Name", guild.Name, true)
121-
.AddField("ID", guild.Id, true)
122-
.WithThumbnailUrl(guild.IconUrl)
121+
.AddField("Name", args.Guild.Name, true)
122+
.AddField("ID", args.Guild.Id, true)
123+
.WithThumbnailUrl(args.Guild.IconUrl)
123124
.WithColor(0xFF0000)
124125
.SendToAsync(channel);
125126
}

src/Services/LoggingService.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Threading.Tasks;
44
using Discord;
55
using Volte.Data.Objects;
6+
using Volte.Data.Objects.EventArgs;
67
using Volte.Extensions;
78
using Console = Colorful.Console;
89
using Color = System.Drawing.Color;
@@ -15,11 +16,10 @@ public sealed class LoggingService
1516
{
1617
private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
1718

18-
internal async Task Log(LogMessage msg)
19+
internal async Task Log(LogEventArgs args)
1920
{
20-
if (msg.Message.Contains("handler is blocking")) return;
21-
var m = Data.Objects.LogMessage.FromDiscordLogMessage(msg);
22-
await Log(m.Severity, m.Source, m.Message, m.Exception);
21+
await Log(args.LogMessage.Internal.Severity, args.LogMessage.Internal.Source,
22+
args.LogMessage.Internal.Message, args.LogMessage.Internal.Exception);
2323
}
2424

2525
internal async Task PrintVersion()

src/Services/VerificationService.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Threading.Tasks;
22
using Discord;
33
using Discord.WebSocket;
4+
using Volte.Data.Objects.EventArgs;
45

56
namespace Volte.Services
67
{
@@ -17,17 +18,16 @@ public VerificationService(DatabaseService databaseService,
1718
_emoji = emojiService;
1819
}
1920

20-
public async Task CheckReactionAsync(Cacheable<IUserMessage, ulong> message, ISocketMessageChannel channel,
21-
SocketReaction reaction)
21+
public async Task CheckReactionAsync(ReactionAddedEventArgs args)
2222
{
23-
if (channel is IDMChannel) return;
24-
if (!(reaction.User.Value is IGuildUser u) || !(channel is IGuildChannel c)) return;
23+
if (args.Channel is IDMChannel) return;
24+
if (!(args.Reaction.User.Value is IGuildUser u) || !(args.Channel is IGuildChannel c)) return;
2525
var config = _db.GetConfig(c.Guild);
26-
if (!config.VerificationOptions.Enabled || !reaction.User.IsSpecified) return;
26+
if (!config.VerificationOptions.Enabled || !args.Reaction.User.IsSpecified) return;
2727
if (u.IsBot) return;
28-
if (message.Id.Equals(config.VerificationOptions.MessageId))
28+
if (args.Message.Id.Equals(config.VerificationOptions.MessageId))
2929
{
30-
if (reaction.Emote.Name.Equals(_emoji.BALLOT_BOX_WITH_CHECK))
30+
if (args.Reaction.Emote.Name.Equals(_emoji.BALLOT_BOX_WITH_CHECK))
3131
{
3232
var role = c.Guild.GetRole(config.VerificationOptions.RoleId);
3333
if (role is null) return;

0 commit comments

Comments
 (0)