-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiscordBot.cs
86 lines (71 loc) · 2.71 KB
/
DiscordBot.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Discord;
using Discordie.Commands;
namespace Discordie
{
/// <summary>
/// Represents a Discord bot
/// </summary>
public class DiscordBot
{
/// <summary>
/// The <see cref="DiscordClient"/> of the current <see cref="DiscordBot"/>.
/// </summary>
public DiscordClient Client { get; internal set; }
/// <summary>
/// The <see cref="CommandParser"/> that will parse all the <see cref="Command"/>s of the current <see cref="DiscordBot"/>.
/// </summary>
public CommandParser Parser { get; internal set; }
/// <summary>
/// Creates a new <see cref="DiscordBot"/>.
/// </summary>
/// <param name="token">The token of the bot.</param>
public DiscordBot(string token) : this(token, new Command[] { }, null)
{
}
/// <summary>
/// Creates a new <see cref="DiscordBot"/> running the given <see cref="Command"/>s.
/// </summary>
/// <param name="token">The token of the bot.</param>
/// <param name="commands">The <see cref="Command"/>s of the bot.</param>
public DiscordBot(string token, IEnumerable<Command> commands) : this(token, commands, null)
{
}
/// <summary>
/// Creates a new <see cref="DiscordBot"/> running the given <see cref="Command"/>s.
/// </summary>
/// <param name="token">The token of the bot.</param>
/// <param name="commands">The <see cref="Command"/>s of the bot.</param>
/// <param name="config">The <see cref="DiscordConfig"/> of the bot.</param>
public DiscordBot(string token, IEnumerable<Command> commands, DiscordConfig config)
{
if (commands == null)
throw new ArgumentNullException(nameof(commands));
Parser = new CommandParser(commands);
if (config != null)
Client = new DiscordClient(config);
else
Client = new DiscordClient();
Client.ExecuteAndWait(async () => {
Client.MessageReceived += GotMessage;
await Client.Connect(token, TokenType.Bot);
});
}
private void GotMessage(object sender, MessageEventArgs e)
{
Parser.ParseInput(e);
}
/// <summary>
/// Adds a new <see cref="Command"/> to the current <see cref="Parser"/>.
/// </summary>
/// <param name="cmd">The command to be added.</param>
public void AddCommand(params Command[] cmd)
{
Parser.Add(cmd);
}
}
}