3

我在 C# 中使用 Discord.Net,制作一个机器人。到目前为止,我的机器人运行良好,但我希望它在用户加入特定服务器时自动为其分配特定角色。我从来没有真正学过任何 C#,只学过一点 C++,所以我知道基本的语法。我该怎么办?我假设我会使用 UserJoined,但这样做会注意结果告诉我在 += 或 -+ 之前或之后使用它(我理解,但我不明白它在给定场景中的用处)

4

3 回答 3

6

您提供的信息很少,但这里是如何在所有版本中执行此操作(到目前为止):

这是在依赖映射中,但在“handlecommand”、CommandHandleAsync 或 HandleCommandAsync 之下:

client.UserJoined += AnnounceJoinedUser; //Hook into the UserJoined event of the client.

这是在依赖映射下:

public async Task AnnounceJoinedUser(SocketGuildUser user) //Welcomes the new user
{
    var channel = client.GetChannel(/*/TextChannelID/*/) as SocketTextChannel; // Gets the channel to send the message in
    await channel.SendMessageAsync($"Welcome {user.mention} to {channel.Guild.Name}"); //Welcomes the new user
} 
于 2017-05-09T06:47:55.817 回答
1

如果你们中的任何人想直接向加入的用户发送消息

client.UserJoined += HandleUserJoinedAsync;
private async Task HandleUserJoinedAsync(SocketGuildUser gUser)
{
    if (gUser.IsBot || gUser.IsWebhook) return;
    var dmChannel = await gUser.GetOrCreateDMChannelAsync();
    await dmChannel.SendMessageAsync("Witaj");
}

于 2020-03-17T17:57:22.577 回答
0

对于所有需要答案的人,在此期间,我留给您这段代码,只是为了向用户的加入发送消息,(1行):

Client.UserJoined += join;

private async Task join(SocketGuildUser user)
{
    await (user.Guild.DefaultChannel).SendMessageAsync("Text")
    return;
}
于 2020-07-30T21:26:40.467 回答