【问题标题】:Discord Bot Event for Members Joining Channel会员加入频道的 Discord Bot 活动
【发布时间】: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 事件,该事件似乎仅在新成员加入频道时触发。我需要能够在任何人登录频道时触发的东西,甚至是现有成员。

【问题讨论】:

标签: c# discord discord.net


【解决方案1】:

从编辑来看,我认为您可能对频道的工作方式有一些误解。

用户加入公会后,就成为公会的一员。
加入公会后,他们就是其中的一员,他们可以看到的频道。因此,不再需要登录频道

现在我认为您想要实现的是在用户从 offline 状态切换到 online 状态时在频道 / 中向用户发送消息。

为此,您可以使用UserUpdated 事件。您可以在其中检查用户以前和当前的状态,并相应地发送消息。

_client.UserUpdated += async (before, after) =>
{
   // Check if the user was offline, and now no longer is
   if(before.Status == UserStatus.Offline && after.Status != UserStatus.Offline)
   {
      // Find some channel to send the message to
      var channel = e.Server.FindChannels("Hello-World", ChannelType.Text);
      // Send the message you wish to send
      await channel.SendMessage(after.Name + " has come online!");
   }
}

【讨论】:

    猜你喜欢
    • 2020-11-04
    • 2020-07-14
    • 2020-11-03
    • 1970-01-01
    • 2022-01-23
    • 2021-01-05
    • 2020-06-26
    • 2017-06-22
    • 2020-07-06
    相关资源
    最近更新 更多