-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
112 lines (92 loc) · 3.26 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
#define DEBUG
//#undef DEBUG
#undef DEFAULTDIR
using Discord;
using Discord.Audio;
using Discord.Commands;
using Discord.WebSocket;
using DiscordBot.Commands;
using DiscordBot.Logging;
using DiscordBot.Utilities.Managers.Storage;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
//using DiscordBot.Utilities.Trivia;
namespace DiscordBot
{
public class Program
{
#if DEFAULTDIR
public const string DIRECTORY = "C:/Users/GABRIEL/Desktop/LangFiles/C#/DiscordBot/DiscordBot";
#else
public const string DIRECTORY = "D:/Gabriel/LangFiles/DiscordBot/DiscordBot";
#endif
private readonly string tokenDir = $"{DIRECTORY}/Token.txt";
private DiscordSocketClient client;
private LoggingService loggingService;
private CommandHandler commandHandler;
private CommandService commandService;
public static void Main(string[] args)
=> new Program().MainAsync().GetAwaiter().GetResult();
public async Task MainAsync()
{
await Login();
client.Ready += WhenReady;
DataStorageManager.Current.LoadData();
DataStorageManager.Current.LoadNewQuestionsFromJson();
// Block this task until the program is closed.
await Task.Delay(-1);
}
private async Task Login()
{
client = new DiscordSocketClient(new DiscordSocketConfig { MessageCacheSize = 100 });
commandService = new CommandService();
loggingService = new LoggingService(client, commandService);
commandHandler = new CommandHandler(client, commandService);
await commandHandler.InstallCommandsAsync();
await client.LoginAsync(TokenType.Bot, File.ReadAllText(tokenDir));
await client.StartAsync();
#if DEBUG
client.MessageUpdated += MessageUpdated;
client.MessageReceived += MessageReceived;
#endif
}
private async Task WhenReady()
{
Console.WriteLine("Bot is connected!");
IVoiceChannel channel;
while (true)
{
try
{
channel = client.Guilds.First().VoiceChannels.First();
break;
}
catch (InvalidOperationException) { }
}
IAudioClient audioClient = await channel.ConnectAsync();
AudioOutStream stream = audioClient.CreateOpusStream();
stream.Write(new byte[1000], 0, 1000);
await client.SetGameAsync("o henrique pela janela");
}
#if DEBUG
private async Task MessageReceived(SocketMessage msg)
{
await Task.Run(() =>
{
Console.WriteLine($"({msg.Channel}, {msg.Author}) -> {msg.Content}");
return Task.CompletedTask;
});
}
private async Task MessageUpdated(Cacheable<IMessage, ulong> before, SocketMessage after, ISocketMessageChannel channel)
{
await Task.Run(async () =>
{
var message = await before.GetOrDownloadAsync();
Console.WriteLine($"({message.Channel}, {message.Author}): {message} -> {after}");
});
}
#endif
}
}