Ejemplo n.º 1
0
        /// <summary>
        /// Called whenever a message is send
        /// </summary>
        private static async void Gateway_OnMessageCreated(object sender, MessageEventArgs e)
        {
            DiscordMessage message = e.Message;

            if (message.Content.StartsWith("!"))
            {
                #region preparing cmdinfo
                // Ignore messages created by this bot or any other bots
                if (message.Author.IsBot)
                {
                    return;
                }

                Shard shard = e.Shard;

                // Grab the DM or guild text channel this message was posted in from cache.
                ITextChannel textChannel = (ITextChannel)shard.Cache.GetChannel(message.ChannelId);

                // Ignore all commands not from servers
                if (textChannel.ChannelType != DiscordChannelType.Guild)
                {
                    return;
                }

                // Visually represent that the bot is working on the command
                await textChannel.TriggerTypingIndicator();

                // Split message into command and arguments
                string[] splitmsg  = message.Content.Split(' ');
                string   command   = splitmsg[0].Substring(1).ToLower();
                string[] arguments = new string[splitmsg.Length - 1];
                Array.Copy(splitmsg, 1, arguments, 0, splitmsg.Length - 1);

                // Retrieve guild- and authorIDs
                ulong guildID  = ((DiscordGuildTextChannel)shard.Cache.GetChannel(message.ChannelId)).GuildId.Id;
                ulong authorID = message.Author.Id.Id;

                // Get a userProfile for the author
                UserProfile authorProfile = new UserProfile(authorID, guildID);

                // Ignore messages made by ignored users
                if (authorProfile.IsIgnored)
                {
                    return;
                }

                // Build a CommandInformation struct used to call commands
                Commands.CommandInformation cmdinfo = new Commands.CommandInformation()
                {
                    Author    = authorProfile,
                    Guild     = new GuildProfile(guildID),
                    Arguments = arguments,
                    Message   = message,
                    Shard     = shard,
                    Messaging = new Messaging(textChannel)
                };
                #endregion

                // Find the command the user requested
                foreach (var cmd in AllCommands)
                {
                    if (command == cmd.Name.ToLower())
                    {
                        // Log the send command asynchronously to keep respond time low
                        log.EnterAsync($"{message.Author.Username} sent command: '{message.Content}'");

                        if (authorProfile.PermissionLevel >= cmd.PermsRequired)
                        {
                            try
                            {
                                await cmd.RunCommand(cmdinfo);
                            }
                            catch (Commands.StopNowException stopNow)
                            {
                                throw stopNow;
                            }
                            catch (Exception exception)
                            {
                                log.Enter(exception, $"processing command '{message.Content}'");
                                await cmdinfo.Messaging.Send("The SaftBot ran into a problem processing your command. If this has happend before, " +
                                                             "please make a bug report here: https://github.com/loglob/SaftbotNET/issues");
                            }
                        }
                        else
                        {
                            await cmdinfo.Messaging.NoPerms();
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
 internal override string InternalRunCommand(CommandInformation cmdinfo)
 {
     return(Utility.SystemSummary());
 }
Ejemplo n.º 3
0
 internal override string InternalRunCommand(CommandInformation cmdinfo)
 {
     return($"{cmdinfo.MentionAuthor} , your ID is {cmdinfo.Author.UserID}");
 }
Ejemplo n.º 4
0
        internal override string InternalRunCommand(CommandInformation cmdinfo)
        {
            Random random = new Random();

            return(answers[(random.Next(00, answers.Length))]);
        }
Ejemplo n.º 5
0
 internal override string InternalRunCommand(CommandInformation cmdinfo)
 {
     cmdinfo.Message.Delete();
     return($"{String.Join(" ", cmdinfo.Arguments)}");
 }
Ejemplo n.º 6
0
 internal override string InternalRunCommand(CommandInformation cmdinfo)
 {
     return("My source can be found at https://github.com/LordGruem/SaftbotNET");
 }
Ejemplo n.º 7
0
        internal override string InternalRunCommand(CommandInformation cmdinfo)
        {
            TimeSpan timeSincePost = DateTime.Now - cmdinfo.Message.Timestamp;

            return($"{cmdinfo.MentionAuthor} Pong! Took {timeSincePost.TotalMilliseconds} ms");
        }
Ejemplo n.º 8
0
 /// <summary>
 /// If RunCommand() is not overridden, all arguments described in Usage with as necessary
 /// (<...> and not [<...>])
 /// </summary>
 /// <param name="cmdinfo"></param>
 /// <returns>The response for the user</returns>
 internal virtual string InternalRunCommand(CommandInformation cmdinfo)
 {
     throw new NotImplementedException();
 }