Ejemplo n.º 1
0
        private async Task HandleCommandAsync(SocketMessage arg)
        {
            // Bail out if it's a System Message.
            var msg = arg as SocketUserMessage;

            if (msg == null)
            {
                return;
            }

            // We don't want the bot to respond to itself or other bots.
            if (msg.Author.Id == _client.CurrentUser.Id || msg.Author.IsBot)
            {
                return;
            }

            // Create a number to track where the prefix ends and the command begins
            int pos = 0;
            // Replace the '!' with whatever character
            // you want to prefix your commands with.
            // Uncomment the second half if you also want
            // commands to be invoked by mentioning the bot instead.
            //if (msg.HasCharPrefix('!', ref pos) /* || msg.HasMentionPrefix(_client.CurrentUser, ref pos) */)
            //{
            // Create a Command Context.
            //var context = new SocketCommandContext(_client, msg);

            // Execute the command. (result does not indicate a return value,
            // rather an object stating if the command executed successfully).
            //var result = await _commands.ExecuteAsync(context, pos, _services);

            // Uncomment the following lines if you want the bot
            // to send a message if it failed.
            // This does not catch errors from commands with 'RunMode.Async',
            // subscribe a handler for '_commands.CommandExecuted' to see those.
            //if (!result.IsSuccess && result.Error != CommandError.UnknownCommand)
            //    await msg.Channel.SendMessageAsync(result.ErrorReason);
            //}

            SocketGuildChannel guildChannel = msg.Channel as SocketGuildChannel;

            if (guildChannel != null)
            {
                if (msg.HasMentionPrefix(_client.CurrentUser, ref pos) && msg.Content.Contains("link"))
                {
                    await msg.Channel.SendMessageAsync("Please DM me your link key");

                    LinkRequest lr = new LinkRequest();
                    lr.author  = msg.Author.Id;
                    lr.channel = msg.Channel.Id;
                    lr.server  = guildChannel.Guild.Id;
                    lock (requests)
                    {
                        requests.Add(lr);
                    }
                }
                else
                {
                    ulong linkkey = database.GetLinkFromChannel(msg.Channel.Id);
                    if (linkkey != 0)
                    {
                        Console.WriteLine("[Discord->Server:" + linkkey + "] [" + msg.Author.Username + "] " + msg.Content);
                        sendToDMP(linkkey, "[" + msg.Author.Username + "] " + msg.Content);
                    }
                    else
                    {
                        Console.WriteLine("[Discord->Server:Unlinked] " + msg.Content);
                    }
                }
            }

            SocketDMChannel dMChannel = msg.Channel as SocketDMChannel;

            if (dMChannel != null)
            {
                ulong linkkey = 0;
                if (ulong.TryParse(msg.Content, out linkkey))
                {
                    LinkRequest linkRequest = null;
                    foreach (LinkRequest lr in requests)
                    {
                        if (lr.author == msg.Author.Id)
                        {
                            linkRequest = lr;
                            Console.WriteLine("Linking " + lr.channel + " to " + linkkey);
                            database.SetLink(lr.server, lr.channel, linkkey);
                        }
                    }
                    if (linkRequest != null)
                    {
                        lock (requests)
                        {
                            requests.Remove(linkRequest);
                        }
                        await msg.Channel.SendMessageAsync("Thankyou! You are now be linked.");
                    }
                    else
                    {
                        await msg.Channel.SendMessageAsync("You must type '@DiscordMultiPlayer link' in the channel you wish to link");
                    }
                }
                else
                {
                    await msg.Channel.SendMessageAsync("Type only just the link key number as the message.");
                }
            }
        }