【发布时间】:2018-12-03 10:39:34
【问题描述】:
我希望我的 Discord 机器人在会员加入频道时向他们打招呼。我一直无法找到发生这种情况时触发的事件。我试过myClient.UserJoined += MyMethod; 和其他人,但他们从来没有像我希望的那样被解雇。这是我的主要代码:
public class Program
{
private DiscordSocketClient _client;
private CommandService _commands;
private IServiceProvider _services;
static void Main(string[] args)
=> new Program().RunBotAsync().GetAwaiter().GetResult();
public async Task RunBotAsync()
{
_client = new DiscordSocketClient();
_commands = new CommandService();
_services = new ServiceCollection()
.AddSingleton(_client)
.AddSingleton(_commands)
.BuildServiceProvider();
string botToken = // removed
_client.Log += Log;
await RegisterCommandsAsync();
await _client.LoginAsync(TokenType.Bot, botToken);
await _client.StartAsync();
await Task.Delay(-1);
}
private Task Log(LogMessage arg)
{
Console.WriteLine(arg);
return Task.CompletedTask;
}
public async Task RegisterCommandsAsync()
{
_client.MessageReceived += HandleCommandAsync;
_client.UserJoined += JoinedAsync; // Something like this to notify bot when someone has joined chat?
await _commands.AddModulesAsync(Assembly.GetEntryAssembly());
}
private Task JoinedAsync(SocketGuildUser arg)
{
throw new NotImplementedException();
}
private async Task HandleCommandAsync(SocketMessage arg)
{
var message = arg as SocketUserMessage;
if(message is null || message.Author.IsBot)
{
return;
}
int argPos = 0;
if (message.HasStringPrefix("!", ref argPos))
{
var context = new SocketCommandContext(_client, message);
await _commands.ExecuteAsync(context, argPos);
}
}
}
谢谢,如果我能提供更多信息,请告诉我。
编辑:建议的链接实现了 UserJoined 事件,该事件似乎仅在新成员加入频道时触发。我需要能够在任何人登录频道时触发的东西,甚至是现有成员。
【问题讨论】:
-
@MXD 谢谢,但我认为这不是我想要的。见编辑。
标签: c# discord discord.net