为什么我的bot dm用户无法收到欢迎消息?(Discord.net C#) [英] Why can't my bot dm users a welcome message? (Discord.net C#)
问题描述
过去几天,我一直在开发discord机器人,并且设法使其中一个功能正常工作:(一个命令,该消息设置了一条消息,通知用户加入时该消息应该发送给dm).我似乎无法让该机器人发送实际的消息.
I've been working on a discord bot for the past few days, and I managed to get one of the functions working: (a command that sets a message it's supposed to dm users when they join). I can't seem to get the bot to send the actual message.
private async Task Join(SocketGuildUser UID)
{
if (UID.IsBot || UID.IsWebhook) return;
Welcometxt= File.ReadAllText([FILE]);
await UID.SendMessageAsync("Your Message Was Sucessfully set!");
}
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);
}
}
当我检查日志时,它给出了带有var消息和上下文的空引用错误,我尝试使用Google搜索错误并更改代码,但没有任何建议?我相信错误是这两种方法之一,但不是100%肯定的
When i check the logs it gives a null reference error with the var message and context, I tried googling the error and changing up the code but to no avail any advice? I believe the error is in one of these two methods but im not 100% positive
推荐答案
我想您可以使用 UserJoined
事件来实现这一目标.
I guess you can use UserJoined
event to achieve this.
为UserJoined定义事件处理程序
public async Task UserJoined(SocketGuildUser user)
{
await user.SendMessageAsync("Hello");
}
注册
private readonly DiscordSocketClient _client = new DiscordSocketClient();
private readonly CommandService _commandService = new CommandService();
public async Task MainAsync()
{
....
_client.UserJoined += UserJoined;
....
}
我不确定是否与此相关,但是请检查服务器成员意图是否处于启用状态
I'm not sure enough if this is related, but check if the Server Member Intents is on
这篇关于为什么我的bot dm用户无法收到欢迎消息?(Discord.net C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!