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 
94   Channel userVoiceChannel(Guild guild, User user) {
95     this.log.infof("k: %s", guild.voiceStates.keys);
96     this.log.infof("v: %s", guild.voiceStates.values);
97 
98     auto state = guild.voiceStates.pick(s => s.userID == user.id);
99     if (!state) return null;
100     return state.channel;
101   }
102 }
103 
104 
105 void main(string[] args) {
106   static if (is(typeof(registerMemoryErrorHandler)))
107       registerMemoryErrorHandler();
108 
109   if (args.length <= 1) {
110     writefln("Usage: %s <token>", args[0]);
111     return;
112   }
113 
114   BotConfig config;
115   config.token = args[1];
116   config.cmdPrefix = "";
117   Bot bot = new Bot(config, LogLevel.trace);
118   bot.loadPlugin(new BasicPlugin);
119   bot.run();
120   runEventLoop();
121   return;
122 }