【发布时间】:2018-12-13 11:59:47
【问题描述】:
我正在使用 Discord.net API 构建一个机器人。机器人的功能之一是欢迎新用户。这是通过与 UserJoined 事件挂钩的事件处理程序完成的。
public class Program
{
private DiscordSocketClient client;
public async Task MainAsync()
{
client = new DiscordSocketClient(new DiscordSocketConfig
{
LogLevel = LogSeverity.Debug
});
client.UserJoined += Client_AnnounceJoinUser;
}
public async Task Client_AnnounceJoinUser(SocketGuildUser guildUser)
{
Console.WriteLine($"[{DateTime.Now} at AnnounceJoinUser] a new user has joined!");
var guild = client.GetGuild(//Guild Id);
var channel = client.GetChannel(//Channel Id) as SocketTextChannel;
var rookieRole = guild.GetRole(//Role Id);
await guildUser.AddRoleAsync(rookieRole);
await channel.SendMessageAsync($"Hello {guildUser.Mention}, and welcome to {channel.Guild.Name}!/n/n{GetMessage(0)}");
}
当用户被添加到公会时,我会收到来自网关确认的日志记录。
[2018 年 12 月 8 日晚上 10:01:30 在网关] 收到派送 (GUILD_MEMBER_ADD)
但是,调用事件处理程序的 client.UserJoined 行永远不会运行。我尝试在此处放置断点,但从未触发断点。我推测问题可能在于未能指定行会来观察 UserJoined 事件,但我不确定如何继续。任何建议将不胜感激。
【问题讨论】:
-
你不需要向
Client_AnnounceJoinUser函数传递一些东西吗? -
由于
public async Task MainAsync()没有返回,发布的代码无法编译。 -
如果你能提供minimal reproducible example就太好了
标签: c# discord.net