-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
160 lines (137 loc) · 7 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
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
156
157
158
159
160
using DisCatSharp;
using DisCatSharp.ApplicationCommands;
using DisCatSharp.CommandsNext;
using DisCatSharp.EventArgs;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
namespace BotName
{
class Program
{
static void Main(string[] args)
{
// dotnet publish -c Release -r linux-arm64 --self-contained=true -p:PublishSingleFile=true -p:GenerateRuntimeConfigurationFiles=true
System.IO.Directory.CreateDirectory("temp");
MainAsync().GetAwaiter().GetResult();
}
#region addSlashCommands
private static Task Client_GuildDownloadCompleted(DiscordClient sender, GuildDownloadCompletedEventArgs guildDownloadCompletedEventArgs)
{
_ = Task.Run(async () =>
{
var appCommandModule = typeof(ApplicationCommandsModule);
var slashCommands = Assembly.GetExecutingAssembly().GetTypes()
.Where(t => appCommandModule.IsAssignableFrom(t) && !t.IsNested).ToList();
var ac = sender.GetApplicationCommands();
sender.Logger.Log(LogLevel.Information, "Guilds: {guildCount}", guildDownloadCompletedEventArgs.Guilds.Count);
foreach (var command in slashCommands)
{
foreach (var guildId in guildDownloadCompletedEventArgs.Guilds.Keys)
{
ac.RegisterGuildCommands(command, guildId);
}
}
await ac.RefreshCommandsAsync();
});
return Task.CompletedTask;
}
#endregion addSlashCommands
static async Task MainAsync()
{
LogLevel logLevel = LogLevel.Information;
logLevel = ConfigurationManager.ConnectionStrings["Loglevel"].ConnectionString switch
{
"Trace" => LogLevel.Trace,
"Debug" => LogLevel.Debug,
"Information" => LogLevel.Information,
_ => LogLevel.Information,
};
var discord = new DiscordShardedClient(new DiscordConfiguration()
{
Token = ConfigurationManager.ConnectionStrings["Token"].ConnectionString,
TokenType = TokenType.Bot,
Intents = DiscordIntents.All,
LogTimestampFormat = "MMM dd yyyy - hh:mm:ss tt",
MinimumLogLevel = logLevel
});
var services = new ServiceCollection().BuildServiceProvider();
var commands = discord.UseCommandsNextAsync(new CommandsNextConfiguration()
{
StringPrefixes = new[] { "," },
EnableMentionPrefix = true,
ServiceProvider = services
});
commands.Result.RegisterCommands(Assembly.GetExecutingAssembly());
var appCommands = discord.UseApplicationCommandsAsync();
#region Eventlistener Registration
discord.ClientErrored += async (client, args) =>
{
await client.GetGuildAsync(869686963828035644).Result.GetChannel(954735094231339099).GetWebhooksAsync().Result[0].ExecuteAsync(new DisCatSharp.Entities.DiscordWebhookBuilder()
{
Content = $"<@724702329693274114>, Error happened! {DateTime.Now}",
}.AddEmbed(new DisCatSharp.Entities.DiscordEmbedBuilder()
{
Title = $"{args.EventName}: {args.Exception.Message}",
Description = args.Exception.StackTrace
} ));
};
discord.SocketErrored += async (client, args) =>
{
await client.GetGuildAsync(869686963828035644).Result.GetChannel(954735094231339099).GetWebhooksAsync().Result[0].ExecuteAsync(new DisCatSharp.Entities.DiscordWebhookBuilder()
{
Content = $"<@724702329693274114>, Error happened! {DateTime.Now}",
}.AddEmbed(new DisCatSharp.Entities.DiscordEmbedBuilder()
{
Title = args.Exception.Message,
Description = args.Exception.StackTrace
}));
};
discord.GuildCreated += async (s, e) =>
{
discord.Logger.Log(LogLevel.Information, "Joined guild '{guildName}' ID: {guildId}", e.Guild.Name, e.Guild.Id);
await Database.Database.Logging.AddLogServer(e.Guild.Id);
var appCommandModule = typeof(ApplicationCommandsModule);
var slashCommands = Assembly.GetExecutingAssembly().GetTypes()
.Where(t => appCommandModule.IsAssignableFrom(t) && !t.IsNested).ToList();
var ac = s.GetApplicationCommands();
foreach (var command in slashCommands)
{
ac.RegisterGuildCommands(command, e.Guild.Id);
}
await ac.RefreshCommandsAsync();
};
discord.GuildDeleted += (s, e) =>
{
discord.Logger.Log(LogLevel.Information, "Left guild '{guildName}' ({guildID})", e.Guild.Name, e.Guild.Id);
return Task.CompletedTask;
};
discord.MessageDeleted += Logging.MessageDeleteEvent.MessageDeleted;
discord.MessageUpdated += Logging.MessageUpdateEvent.MessageUpdated;
discord.MessagesBulkDeleted += Logging.MessageBulkDeleteEvent.MessageBulkDeleted;
discord.ChannelCreated += Logging.ChannelCreateEvent.ChannelCreated;
discord.ChannelDeleted += Logging.ChannelDeleteEvent.ChannelDeleted;
discord.ChannelUpdated += Logging.ChannelUpdateEvent.ChannelUpdated;
discord.ThreadCreated += Logging.ThreadCreateEvent.ThreadCreated;
discord.ThreadUpdated += Logging.ThreadUpdateEvent.ThreadUpdated;
discord.ThreadDeleted += Logging.ThreadDeleteEvent.ThreadDeleted;
discord.GuildBanAdded += Logging.GuildBanAddEvent.GuildBanAdded;
discord.GuildBanRemoved += Logging.GuildBanRemoveEvent.GuildBanRemoved;
discord.GuildEmojisUpdated += Logging.GuildEmojiUpdateEvent.GuildEmojiUpdated;
discord.GuildStickersUpdated += Logging.GuildStickerUpdateEvent.GuildStickerUpdated;
discord.GuildRoleCreated += Logging.GuildRoleCreate.RoleCreated;
discord.GuildRoleUpdated += Logging.GuildRoleUpdate.RoleUpdated;
discord.GuildRoleDeleted += Logging.GuildRoleDelete.RoleDeleted;
discord.InviteCreated += Logging.GuildInviteCreate.InviteCreated;
discord.InviteDeleted += Logging.GuildInviteDelete.InviteDeleted;
discord.GuildDownloadCompleted += (client, e) => Client_GuildDownloadCompleted(client, e);
#endregion Eventlistener Registration
await discord.StartAsync();
await Task.Delay(-1);
}
}
}