Помогите фиксануть код. Задача такова: надо чтобы с моего аккаунта отправлялось определённое сообщение в определённое время, которое я сам введу. Есть некоторые ошибки в коде, заранее извиняюсь за них, болею, голова вообще не думает. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Discord; using Discord.Net; using Discord.WebSocket; namespace ddk { class Program { static void Main(string[] args) { System.Threading.TimerCallback callback = new System.Threading.TimerCallback(); var dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 21, 0, 0); while (true) { if (DateTime.Now < dt) { continue; } else if (DateTime.Now > dt) { continue; } if (DateTime.Now == dt) { } } string token = Console.ReadLine(); } //public async Task Announce() // 1 //{ // DiscordSocketClient _client = new DiscordSocketClient(); // 2 // ulong id = 123456789012345678; // 3 // var chnl = _client.GetChannel(id) as IMessageChannel; // 4 // await chnl.SendMessageAsync("Announcement!"); // 5 //} public async Task SendMessage(string token) { var _client = new DiscordSocketClient(); await _client.LoginAsync(TokenType.Bot, token); await _client.StartAsync(); // this is important // found it here: // https://github.com/discord-net/Discord.Net/issues/1100 _client.Ready += _client_Ready; await Task.Delay(-1); } private async Task _client_Ready() { var guild = _client.GetGuild(7....3); // guild id var channel = guild.GetTextChannel(79034....63); // channel id await channel.SendMessageAsync("my_message"); Environment.Exit(0); } } } Код using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Discord; using Discord.Net; using Discord.WebSocket; namespace ddk { class Program { static void Main(string[] args) { System.Threading.TimerCallback callback = new System.Threading.TimerCallback(); var dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 21, 0, 0); while (true) { if (DateTime.Now < dt) { continue; } else if (DateTime.Now > dt) { continue; } if (DateTime.Now == dt) { } } string token = Console.ReadLine(); } //public async Task Announce() // 1 //{ // DiscordSocketClient _client = new DiscordSocketClient(); // 2 // ulong id = 123456789012345678; // 3 // var chnl = _client.GetChannel(id) as IMessageChannel; // 4 // await chnl.SendMessageAsync("Announcement!"); // 5 //} public async Task SendMessage(string token) { var _client = new DiscordSocketClient(); await _client.LoginAsync(TokenType.Bot, token); await _client.StartAsync(); // this is important // found it here: // https://github.com/discord-net/Discord.Net/issues/1100 _client.Ready += _client_Ready; await Task.Delay(-1); } private async Task _client_Ready() { var guild = _client.GetGuild(7....3); // guild id var channel = guild.GetTextChannel(79034....63); // channel id await channel.SendMessageAsync("my_message"); Environment.Exit(0); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Discord; using Discord.Net; using Discord.WebSocket; using System.Threading; namespace ddk { class Program { private DiscordSocketClient client; private IServiceProvider services; public static string token; static void Main(string[] args) { token = Console.ReadLine(); DateTime date1 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 18, 48, 00); Console.WriteLine(date1); while (true) { Thread.Sleep(1); DateTime date = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second); Console.WriteLine(DateTime.Now); Console.WriteLine(date); if (date1 < date) { continue; } else if (date1 > date) { continue; } if (date1 == date) { //здесь он должен отправить сообщение break; } Console.WriteLine(date); } } public async Task SendMessage(string token) { client = new DiscordSocketClient(); await client.LoginAsync(TokenType.Bearer, token); await client.StartAsync(); client.Ready += _client_Ready; await Task.Delay(-1); } private async Task _client_Ready() { var guild = client.GetGuild(//id); // guild id var channel = guild.GetTextChannel(//id); // channel id await channel.SendMessageAsync("my_message"); Environment.Exit(0); } } } Код using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Discord; using Discord.Net; using Discord.WebSocket; using System.Threading; namespace ddk { class Program { private DiscordSocketClient client; private IServiceProvider services; public static string token; static void Main(string[] args) { token = Console.ReadLine(); DateTime date1 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 18, 48, 00); Console.WriteLine(date1); while (true) { Thread.Sleep(1); DateTime date = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second); Console.WriteLine(DateTime.Now); Console.WriteLine(date); if (date1 < date) { continue; } else if (date1 > date) { continue; } if (date1 == date) { //здесь он должен отправить сообщение break; } Console.WriteLine(date); } } public async Task SendMessage(string token) { client = new DiscordSocketClient(); await client.LoginAsync(TokenType.Bearer, token); await client.StartAsync(); client.Ready += _client_Ready; await Task.Delay(-1); } private async Task _client_Ready() { var guild = client.GetGuild(//id); // guild id var channel = guild.GetTextChannel(//id); // channel id await channel.SendMessageAsync("my_message"); Environment.Exit(0); } } }
Вот новый код, надо реализовать только правильную настройку и отправку сообщения. Не знаю, как можно вызвать async метод и будет ли вообще отправляться сообщение.
Minin_inactive4471240, поменяй сигнатуру метода Main на static async Task Main() и метод вызывай так await SendMessage(token); (не забудь сам метод объявить статическим)
А что сложного? Каждую часть кода тестируй и разберёшься в своей проблеме, не нужный блок кода с while(true) зачем-то. Async чтобы вызвать используй GetAwaiter()
r3xq1, Не понимаю, почему не нужен блок кода while(true) и я так и не смог разобраться с await и async.