试图向新成员发送欢迎信息,但我尝试过的大多数事情似乎根本不起作用

问题描述

这是我已经开始构建的 discord 机器人的当前代码库。到目前为止,我可以通过一个单独的文件来响应命令,这些文件要么用术语定义进行响应,要么计算不同的东西。我现在正试图让它在新成员加入时向欢迎频道发送欢迎信息,但我一点运气都没有。总的来说,我对 C# 和 discord.net 还是很陌生,我学到的大部分知识来自于从 youtube 上的不同视频中混合想法以及在这里我无法解决的类似问题的其他答案。任何指针将不胜感激。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using discord;
using discord.Commands;
using discord.WebSocket;
using Microsoft.Extensions.DependencyInjection;

namespace RCMC_Bot
{
    class Program
    {
        readonly DateTime Now = DateTime.Now;
        static void Main(string[] args)
        {
            if (args is null)
            {
                return;
            }

            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();

            _client.UserJoined += AnnounceUserJoin;

            string token = File.ReadAllText("token.txt");

            _client.Log += Client_Log;

            await RegisterCommandAsync();

            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 RegisterCommandAsync()
        {
            _client.MessageReceived += HandleCommandAsync;
            await _commands.AddModulesAsync(Assembly.GetEntryAssembly(),_services);
        }

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

            int argPos = 0;
            if (message.HasstringPrefix("+",ref argPos))
            {
                var result = await _commands.ExecuteAsync(context,argPos,_services);
                if (!result.IsSuccess) Console.WriteLine(result.ErrorReason);
            }
        }
        public async Task AnnounceUserJoin(SocketGuildUser user)
        {
            var embed = new EmbedBuilder();
            var sb = new StringBuilder();
            embed.WithColor(new Color(0,255,0));
            embed.Title = "Welcome To RCMC";
            sb.AppendLine(user.Mention);
            sb.AppendLine("A community dedicated to helping people learn how to customize their NiTrado DayZ servers for consoles.");
            embed.Description = sb.ToString();
            var channel = _client.GetChannel(768081313420017696) as SocketTextChannel;
            await channel.SendMessageAsync(null,false,embed.Build());
            Console.WriteLine(Now.ToString("F") + " " + user.Mention + " user join");
        }
    }
}

解决方法

问题解决了!看来我的意图启用没有正确保存。现在已更正,欢迎消息正在运行。