1 module main;
2 
3 import std.stdio,
4        std.algorithm,
5        std..string,
6        std.format,
7        std.conv,
8        std.array,
9        std.json,
10        std.traits,
11        std.process,
12        core.time;
13 
14 import vibe.core.core;
15 import vibe.http.client;
16 
17 
18 import dcord.core,
19        dcord.util.process,
20        dcord.util.emitter;
21 
22 import core.sys.posix.signal;
23 import etc.linux.memoryerror;
24 
25 import dcord.util..string : camelCaseToUnderscores;
26 
27 class BasicPlugin : Plugin {
28   @Listener!(MessageCreate, EmitterOrder.AFTER)
29   void onMessageCreate(MessageCreate event) {
30     this.log.infof("MessageCreate: %s", event.message.content);
31   }
32 
33   @Command("ping")
34   void onPing(CommandEvent event) {
35     event.msg.reply(format("Pong: %s", event.msg.author.serializeToJSON));
36   }
37 
38   @Command("embed")
39   void onEmbed(CommandEvent event) {
40     auto embed = new MessageEmbed;
41     embed.title = "TESTING";
42     embed.color = 0x77dd77;
43     embed.description = "lol hey man";
44     event.msg.reply(embed);
45   }
46 
47   //An example command that clears messages in the channel
48   @Command("clear")
49   void onClearMessages(CommandEvent event) {
50     uint limit = 100;
51 
52     //This command can take an integer argument.
53     if(event.args.length > 0){
54       try {
55         limit = event.args[0].to!int;
56       }
57       catch(Exception e){
58         event.msg.reply("You must supply a number of messages to clear (100 max).\n```" ~
59         this.bot.config.cmdPrefix ~ "clear <number>```");
60         return;
61       }
62     }
63 
64     //Delete the command message itself
65     event.msg.del();
66 
67     try {
68       Message[] messages = this.client.getMessages(event.msg.channelID, limit, event.msg.id);
69 
70       if(messages.length > 0){
71         this.client.deleteMessages(event.msg.channelID, messages);
72 
73         event.msg.replyf("I deleted %s messages for you.", messages.length).after(3.seconds).del();
74       }
75     }
76     catch(Exception e){
77       event.msg.replyf("%s", e.msg);
78       return;
79     }
80 
81   }
82 
83   @Command("whereami")
84   void onWhereAmI(CommandEvent event) {
85     auto chan = this.userVoiceChannel(event.msg.guild, event.msg.author);
86     if (chan) {
87       event.msg.reply(format("You're in channel `%s`", chan.name));
88     } else {
89       event.msg.reply("You are not in a voice channel!");
90     }
91   }
92 
93   @Command("spam")
94   void spam(CommandEvent event) {
95     for (int i = 0; i < 30; i++) {
96       this.client.updateStatus(0, new Game(format("Test #%s", i)));
97       sleep(250.msecs);
98       this.log.infof("%s", i);
99     }
100   }
101 
102   Channel userVoiceChannel(Guild guild, User user) {
103     this.log.infof("k: %s", guild.voiceStates.keys);
104     this.log.infof("v: %s", guild.voiceStates.values);
105 
106     auto state = guild.voiceStates.pick(s => s.userID == user.id);
107     if (!state) return null;
108     return state.channel;
109   }
110 }
111 
112 
113 void main(string[] args) {
114   static if (is(typeof(registerMemoryErrorHandler)))
115       registerMemoryErrorHandler();
116 
117   if (args.length <= 1) {
118     writefln("Usage: %s <token>", args[0]);
119     return;
120   }
121 
122   BotConfig config;
123   config.token = args[1];
124   config.cmdPrefix = "";
125   Bot bot = new Bot(config, LogLevel.trace);
126   bot.loadPlugin(new BasicPlugin);
127   bot.run();
128   runEventLoop();
129   return;
130 }