[Command("sendcodes")]
[RequireUserPermission(GuildPermission.Administrator)]
public async Task SendCodesCommand(ulong messageId, string emoji)
{
var message = await Context.Channel.GetMessageAsync(messageId) as IUserMessage;
if (message == null)
{
await ReplyAsync("Message with the specified ID was not found.");
return;
}
// Get the list of users who reacted to the message
var users = await message.GetReactionUsersAsync(new Emoji(emoji), int.MaxValue).FlattenAsync();
// Read codes from the file
var codes = File.ReadAllLines("codes.txt").ToList();
// Check if there are enough codes for all users
if (codes.Count < users.Count())
{
await ReplyAsync("Not enough codes for all users.");
return;
}
int i = 0;
foreach (var user in users)
{
// Skip if the user is a bot
if (user.IsBot)
continue;
// Send the code to the user's DM
var dmChannel = await user.GetOrCreateDMChannelAsync();
await dmChannel.SendMessageAsync($"Your code: {codes[i]}");
i++;
}
await ReplyAsync("Codes have been sent to all users who reacted to the message.");
}```
#Are there any issues in my code that need to be fixed?
1 messages · Page 1 of 1 (latest)
user.GetOrCreateDMChannelAsync(); will throw if u get ratelimited/user has DMs closed
try catch
¯_(ツ)_/¯
r
[Command("sendcodes")]
[RequireUserPermission(GuildPermission.Administrator)]
public async Task SendCodesCommand(ulong messageId, string emoji)
{
var message = await Context.Channel.GetMessageAsync(messageId) as IUserMessage;
if (message == null)
{
await ReplyAsync("Message with the specified ID was not found.");
return;
}
// Get the list of users who reacted to the message
var users = await message.GetReactionUsersAsync(new Emoji(emoji), int.MaxValue).FlattenAsync();
// Read codes from the file
var codes = File.ReadAllLines("codes.txt").ToList();
// Check if there are enough codes for all users
if (codes.Count < users.Count())
{
await ReplyAsync("Not enough codes for all users.");
return;
}
int i = 0;
foreach (var user in users)
{
// Skip if the user is a bot
if (user.IsBot)
continue;
try
{
// Try to send the code to the user's DM
var dmChannel = await user.GetOrCreateDMChannelAsync();
await dmChannel.SendMessageAsync($"Your code: {codes[i]}");
}
catch (Exception ex)
{
// Handle any exceptions (e.g., DMs closed, rate limiting)
Console.WriteLine($"Could not send a DM to {user.Username}: {ex.Message}");
}
i++;
}
await ReplyAsync("Codes have been sent to all users who reacted to the message.");
}```
right?
pretty much