public async Task TestMessage(EventContext e) { if (Enum.TryParse(e.Arguments.ToString().ToLower(), true, out EventMessageType type)) { var allmessages = await GetMessage(e.Guild, type, e.Author); EventMessageObject msg = allmessages.FirstOrDefault(x => x.destinationChannel.Id == e.Channel.Id); e.Channel.QueueMessageAsync(msg.message ?? "No message set in this channel"); return; } e.Channel.QueueMessageAsync($"Please pick one of these tags. ```{string.Join(',', Enum.GetNames(typeof(EventMessageType))).ToLower()}```"); }
public async Task <List <EventMessageObject> > GetMessage(IDiscordGuild guild, EventMessageType type, IDiscordUser user) { long guildId = guild.Id.ToDbLong(); List <IDiscordMessageChannel> channels = await guild.GetChannelsAsync(); List <EventMessageObject> output = new List <EventMessageObject>(); using (var context = new MikiContext()) { foreach (IDiscordMessageChannel c in channels) { EventMessage messageObject = await context.EventMessages.FindAsync(c.Id.ToDbLong(), (short)type); if (messageObject == null) { continue; } if (string.IsNullOrEmpty(messageObject.Message)) { continue; } string modifiedMessage = messageObject.Message; List <IDiscordUser> allUsers = await c.GetUsersAsync(); modifiedMessage = modifiedMessage.Replace("-um", user.Mention); modifiedMessage = modifiedMessage.Replace("-uc", (await user.Guild.GetUserCountAsync()).ToString()); modifiedMessage = modifiedMessage.Replace("-u", string.IsNullOrEmpty(user.Nickname) ? user.Username : user.Nickname); modifiedMessage = modifiedMessage.Replace("-ru", allUsers[MikiRandom.Next(0, allUsers.Count)].GetName()); modifiedMessage = modifiedMessage.Replace("-now", DateTime.Now.ToShortDateString()); modifiedMessage = modifiedMessage.Replace("-s", user.Guild.Name); modifiedMessage = modifiedMessage.Replace("-om", (await user.Guild.GetOwnerAsync()).Mention); modifiedMessage = modifiedMessage.Replace("-o", string.IsNullOrEmpty((await user.Guild.GetOwnerAsync()).Nickname) ? (await user.Guild.GetOwnerAsync()).Username : (await user.Guild.GetOwnerAsync()).Nickname); modifiedMessage = modifiedMessage.Replace("-cc", (await user.Guild.GetChannelCountAsync()).ToString()); modifiedMessage = modifiedMessage.Replace("-vc", (await user.Guild.GetVoiceChannelCountAsync()).ToString()); EventMessageObject o = new EventMessageObject() { message = modifiedMessage, destinationChannel = c }; output.Add(o); } return(output); } }