-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
129 lines (116 loc) · 5.06 KB
/
Program.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
using System;
using System.Threading.Tasks;
using System.IO;
using System.Text.RegularExpressions;
using System.Linq;
using DSharpPlus;
using DSharpPlus.SlashCommands;
using DSharpPlus.Entities;
using DSharpPlus.Interactivity;
using DSharpPlus.Interactivity.Enums;
using DSharpPlus.Interactivity.Extensions;
using Serilog;
using Microsoft.Extensions.Logging;
namespace BountyBot;
public class Program
{
private static bool debugging = false;
static string botToken;
static readonly string tokenPath = Directory.GetCurrentDirectory() + "\\token.tok";
const ulong bountyHunterRoleID = 944815621865111632;
const ulong oathMsgId = 947299195289731113;
static void Main(string[] args)
{
if (args.Contains("DEBUG"))
debugging = true;
const string templateString = "[{Timestamp:yyyy-MM-dd HH:mm:ss zzz}] [{Level:u4}] {Message:lj}{NewLine}{Exception}";
Log.Logger = new LoggerConfiguration()
.WriteTo.Console(outputTemplate: templateString)
.WriteTo.File("logs\\log.txt", rollingInterval: RollingInterval.Day, flushToDiskInterval: TimeSpan.FromMinutes(1), shared: true, outputTemplate: templateString)
.CreateLogger();
if (!File.Exists(tokenPath))
throw new FileNotFoundException("Please add the bot token to " + tokenPath);
botToken = File.ReadAllText(tokenPath);
Managers.BountyManager.Init();
Managers.GuildManager.Init();
MainAsync(args).GetAwaiter().GetResult();
}
static async Task MainAsync(string[] args)
{
DiscordClient client = new(new DiscordConfiguration()
{
Token = botToken,
TokenType = TokenType.Bot,
Intents = DiscordIntents.All,
LoggerFactory = new LoggerFactory().AddSerilog()
});
var slash = client.UseSlashCommands();
if (debugging)
foreach (var guild in Managers.GuildManager.Guilds.Where(x => x.deployment == false))
{
slash.RegisterCommands<Commands.TopLevelCommands>(guild.id);
slash.RegisterCommands<Commands.BountyCommands>(guild.id);
}
else
foreach (var guild in Managers.GuildManager.Guilds)
{
slash.RegisterCommands<Commands.TopLevelCommands>(guild.id);
slash.RegisterCommands<Commands.BountyCommands>(guild.id);
}
client.UseInteractivity(new()
{
PollBehaviour = PollBehaviour.KeepEmojis,
Timeout = TimeSpan.FromSeconds(30)
});
// Event Listeners
{
// Oath Check (+,-)
client.MessageReactionAdded += AcceptOath;
client.MessageReactionRemoved += RenounceOath;
// Button Interactivity Deferration
//client.ComponentInteractionCreated += async (s, e) => await e.Interaction.CreateResponseAsync(InteractionResponseType.DeferredMessageUpdate);
// Slash Command Errors
slash.SlashCommandErrored += OnError;
}
await client.ConnectAsync(new DiscordActivity("A Bounty Hunt", ActivityType.Competing));
await Task.Delay(-1);
}
public static async Task AcceptOath(DiscordClient _, DSharpPlus.EventArgs.MessageReactionAddEventArgs e)
{
if (e.Message.Id != oathMsgId)
return;
var user = await e.Guild.GetMemberAsync(e.User.Id);
var role = e.Guild.Roles[bountyHunterRoleID];
if (user.Roles.Contains(role))
return;
await user.GrantRoleAsync(role);
Log.Information("User {0} has accepted the oath", user.Username + '#' + user.Discriminator);
}
public static async Task RenounceOath(DiscordClient _, DSharpPlus.EventArgs.MessageReactionRemoveEventArgs e)
{
if (e.Message.Id != oathMsgId)
return;
var user = await e.Guild.GetMemberAsync(e.User.Id);
var role = e.Guild.Roles[bountyHunterRoleID];
if (!user.Roles.Contains(role))
return;
await user.RevokeRoleAsync(role);
Log.Information("User {0} has renounced the oath", user.Username + '#' + user.Discriminator);
}
public static async Task OnError(SlashCommandsExtension s, DSharpPlus.SlashCommands.EventArgs.SlashCommandErrorEventArgs e)
{
if (e.Exception is SlashExecutionChecksFailedException slex)
{
foreach (var check in slex.FailedChecks)
if (check is Attributes.RequireRolesAttribute)
await e.Context.CreateResponseAsync(":x: **You do not have permission to run this command.**", true);
}
else
try { await e.Context.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder().WithContent(":x: **Error**: " + e.Exception.Message)); }
catch
{
await e.Context.DeleteResponseAsync();
await e.Context.FollowUpAsync(new DiscordFollowupMessageBuilder().AsEphemeral().WithContent(":x: **Error**: " + e.Exception.Message));
}
}
}