コード例 #1
0
ファイル: Koala.cs プロジェクト: Lachee/koala-bot
        /// <summary>
        /// Resolves the prefix of the message and returns a index to trim from.
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public async Task <int> ResolvePrefix(DiscordMessage message)
        {
            try
            {
                if (message.Author == null || message.Author.IsBot)
                {
                    return(-1);
                }

                //Get the prefix. If we fail to find the prefix then we will get it from the cache
                //if (!_guildPrefixes.TryGetValue(message.Channel.GuildId, out prefix))
                //{
                //    Logger.Log("Prefix Cache Miss. Fetching new prefix for guild " + message.Channel.GuildId);
                //    prefix = await Redis.FetchStringAsync(Namespace.Combine(message.Channel.GuildId, "prefix"), Configuration.Prefix);
                //    await UpdatePrefix(message.Channel.Guild, prefix);
                //}

                //Get the position of the prefix
                string prefix = await GuildSettings.GetPrefixAsync(message.Channel.Guild);

                var pos = message.GetStringPrefixLength(prefix);
                if (pos >= 0)
                {
                    //Make sure we are allowed to execute in this channel
                    // We want to be able to execute in this channel unless specifically denied.
                    var member = await message.Channel.Guild.GetMemberAsync(message.Author.Id);

                    var state = await member.HasPermissionAsync($"koala.execute.{message.ChannelId}", bypassAdmin : true, allowUnset : true);

                    if (!state)
                    {
                        return(-1);
                    }
                }

                //Return the index of the prefix
                return(pos);
            }
            catch (Exception e)
            {
                this.Logger.LogError(e);
                return(-1);
            }
        }