-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
122 lines (105 loc) · 4.23 KB
/
Copy pathProgram.cs
File metadata and controls
122 lines (105 loc) · 4.23 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Discord.WebSocket;
namespace DabBot_
{
class Program
{
private DiscordSocketClient _client;
public static List<Phrase> phrases = new List<Phrase>();
public static string pathPrefix = "";
public static Task Main(string[] args) => new Program().MainAsync();
public async Task MainAsync()
{
//Create client
_client = new DiscordSocketClient();
#if DEBUG
pathPrefix = @"..\";
#endif
//Login and start the bot
string token = File.ReadAllText(pathPrefix + "token.txt");
await _client.LoginAsync(TokenType.Bot, token);
await _client.StartAsync();
//Listen to event
_client.Ready += Ready;
_client.Log += Log;
_client.MessageReceived += MessageReceived;
CommandHandler commandHandler = new CommandHandler(_client, new CommandService());
await commandHandler.InstallCommandsAsync();
//Block this task until the program is closed.
await Task.Delay(-1);
}
private Task MessageReceived(SocketMessage msg)
{
//Ensure that no bot can trigger a response
if (!msg.Author.IsBot)
{
//Check the message for any known phrases
foreach (Phrase phrase in phrases)
{
//If a match is found
if (phrase.regex.IsMatch(msg.CleanContent))
{
//Randomly select a number corresponding to a file in the list
int random = new Random().Next(0, phrase.links.Count);
Embed embed = new EmbedBuilder
{
ImageUrl = phrase.links[random]
}.Build();
msg.Channel.SendMessageAsync("", embed: embed);
}
}
}
return Task.CompletedTask;
}
private Task Log(LogMessage msg)
{
Console.WriteLine(msg.ToString());
return Task.CompletedTask;
}
private Task Ready()
{
//Read from the existing file if it exists
if (File.Exists(pathPrefix + "phrases.json"))
{
phrases = JsonHelper.DeserializePhrases(pathPrefix);
}
else
{
//If the file doesn't exist, create and populate with some sample data
File.Create(pathPrefix + "phrases.json").Close();
List<string> dabLinks = new List<string>()
{
"http://mrhumagames.com/DabBot/dab/unicorn.png",
"http://mrhumagames.com/DabBot/dab/panda.png",
"http://mrhumagames.com/DabBot/dab/pepe.png",
"http://mrhumagames.com/DabBot/dab/roblox.png",
"http://mrhumagames.com/DabBot/dab/cat.png",
"http://mrhumagames.com/DabBot/dab/dog.png",
};
phrases.Add(new Phrase(new Regex("dab", RegexOptions.IgnoreCase), dabLinks));
List<string> sheeshLinks = new List<string>()
{
"http://mrhumagames.com/DabBot/sheesh/1.png",
"http://mrhumagames.com/DabBot/sheesh/2.png",
"http://mrhumagames.com/DabBot/sheesh/3.png",
"http://mrhumagames.com/DabBot/sheesh/4.png",
"http://mrhumagames.com/DabBot/sheesh/5.png",
};
phrases.Add(new Phrase(new Regex("shee+sh", RegexOptions.IgnoreCase), sheeshLinks));
JsonHelper.SerializePhrases(phrases, pathPrefix);
}
//Displays each phrase and how many images are stored for the phrase
foreach(Phrase phrase in phrases)
{
Console.WriteLine("Listening to " + phrase.regex + " with " + phrase.links.Count + " links.");
}
return Task.CompletedTask;
}
}
}