-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
153 lines (127 loc) · 6.05 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
using Discord;
using Discord.Rest;
using Discord.WebSocket;
using DiscordBot.Games;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace DiscordBot {
public enum DiscardGames {
NoThanks,
IncanGold,
NOTFOUND
}
class Program {
public static void Main(string[] args)
=> new Program().MainAsync().GetAwaiter().GetResult();
private DiscordSocketClient _client;
// Dictionary mapping IDs to active channels
public Dictionary<ulong, ChannelHandler> activeChannels;
public async Task MainAsync() {
DiscordSocketConfig config = new DiscordSocketConfig { MessageCacheSize = 64, GuildSubscriptions = false };
// When Discord.Net.WebSocket receives a stable update to include GatewayIntents, add here
// and unsub from typing, etc. instead of guildsubscriptions
_client = new DiscordSocketClient(config);
_client.MessageReceived += MessageHandler;
_client.ReactionAdded += ReactionAdder;
_client.ReactionRemoved += ReactionRemover;
_client.Log += Log;
// Copy-Paste your token in Environment Variables under User.
// Can select System-wide Env Variables instead of User, just change the 2nd Param for targeting.
var token = Environment.GetEnvironmentVariable("DISCORD_TOKEN", EnvironmentVariableTarget.User);
activeChannels = new Dictionary<ulong, ChannelHandler>();
await _client.LoginAsync(TokenType.Bot, token);
await _client.StartAsync();
await _client.SetStatusAsync(UserStatus.Online);
await _client.SetActivityAsync(new Game("!help", ActivityType.Listening, ActivityProperties.Spectate, "details??"));
// Block this task until the program is closed.
await Task.Delay(-1);
}
private Task Log(LogMessage message) {
Console.WriteLine($"[General/{message.Severity}] {message}");
return Task.CompletedTask;
}
private ChannelHandler GetChannelHandler(ISocketMessageChannel channel) {
// check that we're in a server text channel
if (channel.GetType() != typeof(SocketTextChannel)) {
channel.SendMessageAsync(
"Sorry, **Discard Games Bot** is not available for" +
"channels of type " + channel.GetType());
return null;
}
ulong id = channel.Id;
if (!activeChannels.ContainsKey(id)) {
activeChannels.Add(id, new ChannelHandler(channel));
}
return activeChannels[id];
}
private Task ReactionRouter(ISocketMessageChannel channel, SocketReaction reaction, bool adding) {
// this is a reaction we added ourselves. Ignore it.
if (reaction.UserId == _client.CurrentUser.Id) {
return Task.CompletedTask;
}
ChannelHandler handler = GetChannelHandler(channel);
if (handler != null) {
handler.ProcessReaction(reaction.MessageId, reaction.Emote.Name, reaction.UserId, adding);
}
return Task.CompletedTask;
}
private Task ReactionAdder(Cacheable<IUserMessage, ulong> cache, ISocketMessageChannel channel, SocketReaction reaction) {
return ReactionRouter(channel, reaction, true);
}
private Task ReactionRemover(Cacheable<IUserMessage, ulong> cache, ISocketMessageChannel channel, SocketReaction reaction) {
return ReactionRouter(channel, reaction, false);
}
private Task MessageHandler(SocketMessage message) {
if (!message.Content.StartsWith('!')) {
return Task.CompletedTask;
}
if (message.Author.IsBot) {
return Task.CompletedTask;
}
string command = message.Content.Substring(1).ToLower().Trim();
if (command.StartsWith("test")) {
// any test things do here
} else if (command.StartsWith("help")) {
message.Channel.SendMessageAsync(null, false, HelpMessage());
} else if (command.StartsWith("bugsnax")) {
SendBugsnax(message);
} else {
ChannelHandler handler = GetChannelHandler(message.Channel);
if (handler != null) {
handler.ProcessMessage(message, command);
}
}
return Task.CompletedTask;
}
private Embed HelpMessage() {
EmbedBuilder builder = new EmbedBuilder {
Title = "HELP - Discard Bot",
Description = "List of commands:",
};
builder.WithAuthor(_client.CurrentUser)
.AddField("!help", "gives you this messsage!")
.AddField("!play GameName", "WIP. Hopefully starts a game of GameName for you.")
.AddField("List of games for !play:", "***No Thanks!***, ***Incan Gold***")
.AddField("!rules", "NOT IMPLEMENTED")
.AddField("!status", "NOTIMPLEMENTED")
.WithColor(Color.Orange);
return builder.Build();
}
private void SendBugsnax(SocketMessage message) {
message.Channel.SendMessageAsync("it's bugsnax, " + message.Author.Mention);
message.AddReactionAsync(new Emoji("\U0001f495"));
Task<RestUserMessage> sent = message.Channel.SendFileAsync("bugsnax.jpg");
sent.Result.AddReactionsAsync(new IEmote[] { new Emoji("🇧"), new Emoji("🇺"), new Emoji("🇬") });
}
public static DiscardGames FindGameFromString(string toFind) {
toFind = toFind.Replace(" ", string.Empty);
if (toFind == "nothanks" || toFind == "nothanks!") {
return DiscardGames.NoThanks;
} else if (toFind == "incangold") {
return DiscardGames.IncanGold;
}
return DiscardGames.NOTFOUND;
}
}
}