Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
126 lines (93 loc) · 3.77 KB
/
Copy pathProgram.cs
File metadata and controls
126 lines (93 loc) · 3.77 KB
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
using System;
using System.Threading.Tasks;
using ControlCopy.Commands;
using DSharpPlus;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Exceptions;
using DSharpPlus.Entities;
using DSharpPlus.EventArgs;
using DSharpPlus.Interactivity.Extensions;
using Microsoft.Extensions.Logging;
namespace ControlCopy
{
public class Program
{
public readonly EventId BotEventId = new EventId(42, "ControlCopy");
public DiscordClient Client { get; set; }
public CommandsNextExtension Commands { get; set; }
public static void Main( string[] args )
{
var prog = new Program();
prog.RunBotAsync().GetAwaiter().GetResult();
}
public async Task RunBotAsync()
{
var cfg = new DiscordConfiguration
{
Token = Environment.GetEnvironmentVariable("TOKEN"),
TokenType = TokenType.Bot,
AutoReconnect = true,
MinimumLogLevel = LogLevel.Debug
};
this.Client = new DiscordClient( cfg );
this.Client.Ready += this.Client_Ready;
this.Client.GuildAvailable += this.Client_GuildAvailable;
this.Client.ClientErrored += this.Client_ClientError;
this.Client.UseInteractivity();
var ccfg = new CommandsNextConfiguration
{
StringPrefixes = new[] { "cc." },
EnableDms = false,
EnableMentionPrefix = true
};
this.Commands = this.Client.UseCommandsNext( ccfg );
this.Commands.CommandExecuted += this.Commands_CommandExecuted;
this.Commands.CommandErrored += this.Commands_CommandErrored;
// let's add a converter for a custom type and a name
// var mathopcvt = new MathOperationConverter();
// Commands.RegisterConverter(mathopcvt);
// Commands.RegisterUserFriendlyTypeName<MathOperation>("operation");
this.Commands.RegisterCommands<BasicCommandsModule>();
this.Commands.RegisterCommands<CopyChannelCommand>();
// set up our custom help formatter
// this.Commands.SetHelpFormatter<SimpleHelpFormatter>();
await this.Client.ConnectAsync();
await Task.Delay( -1 );
}
private Task Client_Ready( DiscordClient sender, ReadyEventArgs e )
{
sender.Logger.LogInformation( BotEventId, "Client is ready to process events." );
return Task.CompletedTask;
}
private Task Client_GuildAvailable( DiscordClient sender, GuildCreateEventArgs e )
{
sender.Logger.LogInformation( BotEventId, $"Guild available: {e.Guild.Name}" );
return Task.CompletedTask;
}
private Task Client_ClientError( DiscordClient sender, ClientErrorEventArgs e )
{
sender.Logger.LogError( BotEventId, e.Exception, "Exception occured" );
return Task.CompletedTask;
}
private Task Commands_CommandExecuted( CommandsNextExtension sender, CommandExecutionEventArgs e )
{
e.Context.Client.Logger.LogInformation( BotEventId, $"{e.Context.User.Username} successfully executed '{e.Command.QualifiedName}'" );
return Task.CompletedTask;
}
private async Task Commands_CommandErrored( CommandsNextExtension sender, CommandErrorEventArgs e )
{
e.Context.Client.Logger.LogError( BotEventId, $"{e.Context.User.Username} tried executing '{e.Command?.QualifiedName ?? "<unknown command>"}' but it errored: {e.Exception.GetType()}: {e.Exception.Message ?? "<no message>"}", DateTime.Now );
if ( e.Exception is ChecksFailedException ex )
{
var emoji = DiscordEmoji.FromName(e.Context.Client, ":no_entry:");
var embed = new DiscordEmbedBuilder
{
Title = "Access denied",
Description = $"{emoji} You do not have the permissions required to execute this command.",
Color = new DiscordColor(0xFF0000) // red
};
await e.Context.RespondAsync( embed );
}
}
}
}