コード例 #1
0
        /// <inheritdoc />
        public async Task <bool> MuteAsync(
            SocketGuildUser user,
            SocketUser muter,
            long?duration = null,
            string reason = null)
        {
            DateTimeOffset now = DateTimeOffset.UtcNow;

            if (!await DataBaseUtil.AddMuteAsync(user, muter, now, duration, reason))
            {
                await _data.ChannelLog($"Failure Muting {user}", "User is already muted.");

                return(false);
            }

            await user.AddRoleAsync(
                _data.MuteRole,
                new RequestOptions { AuditLogReason = $"{muter}: {reason}".Truncate(512, true) });

            #region Messages

            var message    = "You have been muted ";
            var logMessage = "Duration: ";

            if (duration.HasValue)
            {
                string expiration = now.AddMinutes(duration.Value).ToString("yyyy-MM-ddTHH:mm:ssZ");

                message    += $"for `{duration}` minute(s) (expires `{expiration}`)";
                logMessage += $"{duration}\nExpires: {expiration}\nMuter: {muter}";
            }
            else
            {
                message    += "indefinitely";
                logMessage += $"indefinitely\nMuter: {muter}";
            }

            if (reason != null)
            {
                message    += $" because:```{reason}```";
                logMessage += $"\nReason: {reason}";
            }
            else
            {
                message += ".";
            }

            try
            {
                // Tries to send a DM.
                await user.SendMessageAsync(message);
            }
            catch
            {
                // Mentions the author in the the general channel instead.
                await _data.GeneralChannel.SendMessageAsync($"Hey {user.Mention}!\n{message}");
            }

            await _data.ChannelLog($"Muted {user}", logMessage);

            #endregion

            return(true);
        }