Im trying to figure out how Im able to run an async in a thread so i can make this more efficient for my discord bot.
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Discord;
using Gommon;
using Qmmands;
using Volte.Core.Entities;
using Volte.Commands;
using Volte.Core.Helpers;
using Volte.Services;
using System.Threading;
using Discord.WebSocket;
namespace Volte.Commands.Modules
{
public sealed partial class BotOwnerModule
{
private readonly DiscordShardedClient _client;
[Command("CreatePoll", "Cp")]
[Description("Creates polls and sends to every server.")]
public Task<ActionResult> CreatePollAync([Remainder, Description("The content of the poll. Format is `question;option1[;option2;option3;option4;option5]`. You do not need to provide the brackets.")] string poll)
{
var content = poll.Split(';', StringSplitOptions.RemoveEmptyEntries);
var pollInfo = PollHelper.GetPollBody(content);
if (!pollInfo.IsValid)
return BadRequest(content.Length - 1 > 5
? "More than 5 options were specified."
: "No options specified.");
var embed = Context.CreateEmbedBuilder()
.WithTitle(Format.Bold(content[0]));
return None(async () =>
{
foreach (var guild in Context.Client.Guilds)
{
new Thread(() => {
Thread.CurrentThread.IsBackground = true;
await Context.Client.GetGuild(guild.Id).GetTextChannel(new DatabaseService(_client).GetData(guild).Configuration.channelid).SendMessageAsync("test");
var m = await pollInfo.Apply(embed).SendToAsync(Context.Client.GetGuild(guild.Id).GetTextChannel(new DatabaseService(_client).GetData(guild).Configuration.channelid));
_ = await Context.Message.TryDeleteAsync("Poll invocation message.");
await PollHelper.AddPollReactionsAsync(pollInfo.Fields.Count, m);
}).Start();
}
});
}
}
}
I have tried a few ways like returning None inside of the thread but that didnt work either so thats why im on here asking for help as i have googled this issue but this exact issue doesnt seem to pop up