How to Send a Message To Discord Channel where ever i want in Discord.Net?

Viewed 32

hi developing a discord bot and i have problem!

*discoord Api:10

Discord.Net 3.8.1

.Net 6*

namespace DNet_V3_Bot
{
    class Program
    {
        static void Main(string[] args) => new Program().RunBotAsync().GetAwaiter().GetResult();

        private DiscordSocketClient _client;
        private CommandService _commands;
        private IServiceProvider _services;

        public async Task RunBotAsync()
        {
            _client = new DiscordSocketClient();
            _commands = new CommandService();

            _services = new ServiceCollection()
                .AddSingleton(_client)
                .AddSingleton(_commands)
                .BuildServiceProvider();

            string token = "myToken";

            _client.Log += _client_Log;

            await RegisterCommandsAsync();

            await _client.LoginAsync(TokenType.Bot, token);

            await _client.StartAsync();
            await Task.Delay(-1);

        }

        private Task _client_Log(LogMessage arg)
        {
            Console.WriteLine(arg);
            return Task.CompletedTask;
        }

        public async Task RegisterCommandsAsync()
        {
            _client.Ready += _client_Ready;
            _client.MessageReceived += HandleCommandAsync;
            await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _services);
        }
        private async Task _client_Ready()
        {
            await SendMessage("Mmd");
        }

        private async Task HandleCommandAsync(SocketMessage arg)
        {
            var message = arg as SocketUserMessage;
            if (message == null) return;
            var context = new SocketCommandContext(_client, message);
            if (message.Author.IsBot) return;
            int argPos = 0;

            // await Announce(message.Channel.Id);
            await SendMessage("Rasool");
            if (message.HasStringPrefix("!", ref argPos))
            {
                var result = await _commands.ExecuteAsync(context, argPos, _services);
                if (!result.IsSuccess) Console.WriteLine(result.ErrorReason);
                if (result.Error.Equals(CommandError.UnmetPrecondition)) await message.Channel.SendMessageAsync(result.ErrorReason);
            }
        }
        public async Task SendMessage(string serverName)
        {
            ulong id = 1...3;
            var chnl = _client.GetChannel(id) as IMessageChannel;
            await chnl.SendMessageAsync($"serverName");
            Console.WriteLine(chnl.Id);
        }
    }
}

for example i want run SendMessage Task where ever I want???

its okay when i call it in client ready but in another palces No!

how can i do it???

its just for stackoverflow Error ignore It. :) In publishing and graphic design, Lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document or a typeface without relying on meaningful content. Lorem ipsum may be used as a placeholder before final copy is available.

Please Help Me!

0 Answers
Related