-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cs
More file actions
75 lines (61 loc) · 2.4 KB
/
Copy pathmain.cs
File metadata and controls
75 lines (61 loc) · 2.4 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
using Discord;
using Discord.WebSocket;
using Discord.Commands;
using System;
using System.Threading.Tasks;
class Program {
public static void Main(string[] args)
=> new Program()
.MainAsync()
.GetAwaiter()
.GetResult();
private DiscordSocketClient client;
public async Task MainAsync()
{
var client = new DiscordSocketClient();
client.MessageReceived += CommandHandler;
client.Log += Log;
var token = Environment.GetEnvironmentVariable("token");
await client.LoginAsync(TokenType.Bot, token);
await client.StartAsync();
await client.SetStatusAsync(UserStatus.Idle);
await client.SetActivityAsync(new Game("Codes in C#", ActivityType.Watching));
await Task.Delay(-1); // Block this task until the program is closed.
}
private Task Log(LogMessage msg)
{
Console.WriteLine(msg.ToString());
return Task.CompletedTask;
}
private Task CommandHandler(SocketMessage message)
{
//variables
string command = "";
int lengthOfCommand = -1;
//filtering messages begin here
var prefix = '!'; // set the prefix
if (!message.Content.StartsWith(prefix)) //message starts with prefix
return Task.CompletedTask;
if (message.Author.IsBot) // ignores all commands from bots
return Task.CompletedTask;
if (message.Content.Contains(' '))
lengthOfCommand = message.Content.IndexOf(' ');
else
lengthOfCommand = message.Content.Length;
command = message.Content.Substring(1, lengthOfCommand - 1).ToLower();
//Commands begin here
if (command.Equals("hello"))
{
message.Channel.SendMessageAsync($@"Hello {message.Author.Mention}");
}
else if (command.Equals("age"))
{
message.Channel.SendMessageAsync($@"Your account was created at {message.Author.CreatedAt.DateTime.Date}");
}
else if (command.Equals("info"))
{
message.Channel.SendMessageAsync($@"{message.Author.Mention}, This bot is code in `C#` by `Nathaniel VFX#6321`");
}
return Task.CompletedTask;
}
}