【问题标题】:give role when a user add reaction Discord.net当用户添加反应时赋予角色 Discord.net
【发布时间】:2019-09-22 01:59:08
【问题描述】:

我正在尝试添加反馈(重击 + 重击)以嵌入到 EmbedBuilder 消息中。如果用户竖起大拇指则将角色“好”应用到他,如果他大拇指向下应用角色“坏”,同样如果删除他的反应类型(thump..up/dawn)将删除他的角色(赢/输)。

message_txt_Embed 是要发送的嵌入消息。

Private Async Function onMsg(message As SocketMessage) As Task
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If message.Source = MessageSource.Bot Then
        'ignore bot repeat message message
Else

   If message.Content = message_txt_Embed.text Then

            Dim msg As String = message.Content
            Dim embed As New EmbedBuilder With {
      .Title = msg,
      .Description = "No Description",
      .Color = New Discord.Color(255, 0, 0)
       }
            Await message.Channel.SendMessageAsync("", False, embed)


            '''''' what i try to add reaction for user's who add reaction

            Dim guild As SocketGuild = (CType(message.Channel, SocketGuildChannel)).Guild
            Dim emote As IEmote = guild.Emotes.First(Function(e) e.Name = ":thumbsup:")
            Await guild.Users.AddReactionAsync(emote)

            ''''''
        End If

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
End If

当我将 message_txt_Embed.text 更改为“test”,然后运行 ​​bot.txt 时,它是如何工作的。如果我输入“test”[as message_txt_Embed.text],然后创建嵌入 msg。

我的观点是:如何在添加反应时为用户应用角色(thump..up/dawn),如果他删除了他的反应,我也可以删除角色。

【问题讨论】:

  • 你能澄清你的意思吗:“当我将 message_txt_Embed.text 更改为“test”,然后运行机器人时,它是如何工作的。如果我输入“test”[as message_txt_Embed.text ],然后创建嵌入味精。"
  • @Anu6is okkey 让它在 bot 运行时为特定的 channel.id "45....." 发送嵌入消息,如果用户添加反应 (thump up) 将为他添加角色“赢”/如果他删除(重击)反应,如果发现“赢”,则从他身上删除角色

标签: vb.net discord.net


【解决方案1】:

首先,我想澄清一些与您的示例代码有关的问题。请参阅下面的 cmets

'This function is only called when a message is recieved by the client.
'It is not going to be called when a user adds a reaction to a message.
'As such, no logic should be included in here in relation to reactions.
Private Async Function onMsg(message As SocketMessage) As Task

If message.Source = MessageSource.Bot Then
        'Instead of having an empty bode in your condition, you could use:

        'If Not message.Source = MessageSource.Bot Then 
        '   execute your logic
        'End If     

        'The code above would remove the need for includeing an ELSE section 
        'and no longer require the IF section to be empty
Else
    If message.Content = message_txt_Embed.text Then
        Dim msg As String = message.Content
        Dim embed As New EmbedBuilder With {
            .Title = msg,
            .Description = "No Description",
            .Color = New Discord.Color(255, 0, 0)
       }

       Await message.Channel.SendMessageAsync("", False, embed)

       Dim guild As SocketGuild = (CType(message.Channel, SocketGuildChannel)).Guild

       'If you are trying to get the standard thumbsup emoji this is not going to work
       'SocketGuild#Emotes is a collection of custom emotes that are available in the guild.
       'If this thumbsup is in fact a custom emote you'd access the name withoug the ':'
       Dim emote As IEmote = guild.Emotes.First(Function(e) e.Name = ":thumbsup:")
       Await guild.Users.AddReactionAsync(emote) 'This is not a valid function.
    End If
End If

解决方案:
- 创建一个 List(Of Ulong),用于存储添加反应时应赋予角色的任何消息的 ID。这样可以确保不会为对频道中的任何消息做出反应而指定角色。
- 使用 ReactionAdded 和 ReactionRemoved 事件来监听用户添加或删除的反应。

您可以在ReactionAdded Event here 上找到文档

'Any time a message is created with the purpose of adding reaction roles
'that message id should be added to this list. 
'This example does not include code for populating this list!
Private ReadOnly ReactionMessages As New List(Of ULong)
'Subscribe to the ReactionAdded and ReactionRemoved events (code not included)

已添加反应 - 添加角色

    Private Async Function ReactionAdded(cache As Cacheable(Of IUserMessage, ULong), channel As ISocketMessageChannel, reaction As SocketReaction) As Task
        If Not reaction.User.IsSpecified Then Return

        'cache.id is the id of the message the user added the reaction to
        'Check if the message the user is reacting to is a valid reaction message
        'If valid, the message id should exist in our ReactionMessages collection
        'Valid reaction messages are the only message that should assign or remove roles
        If ReactionMessages.Contains(cache.Id) Then
            Dim role As IRole = Nothing

            'The unicode string (? and ?) is used when comparing Discord emojis
            If reaction.Emote.Name.Equals("?") Then
                'Retrieve the "good role" from the guild, using the role id
                role = DirectCast(channel, SocketGuildChannel).Guild.GetRole(123456789)
            ElseIf reaction.Emote.Name.Equals("?") Then
                'Retrieve the "bad role" from the guild, using the role id
                role = DirectCast(channel, SocketGuildChannel).Guild.GetRole(987654321)
            End If

            'Only if the role was found within the guild should we attempt to add it to the user
            If role IsNot Nothing Then Await DirectCast(reaction.User.Value, SocketGuildUser).AddRoleAsync(role)
        End If
    End Function

Reaction Removed - 删除角色

    Private Async Function ReactionRemoved(cache As Cacheable(Of IUserMessage, ULong), channel As ISocketMessageChannel, reaction As SocketReaction) As Task
        If Not reaction.User.IsSpecified Then Return

        'cache.id is the id of the message the user is reacting to
        'Check if the message the user is reacting to is a valid reaction message
        'If valid, the message id should exist in our ReactionMessages collection
        'Valid reaction messages are the only message that should assign or remove roles
        If ReactionMessages.Contains(cache.Id) Then
            Dim role As IRole = Nothing
            Dim user As SocketGuildUser = reaction.User.Value

            'The unicode string (? and ?) is used when comparing Discord emojis
            If reaction.Emote.Name.Equals("?") Then
                'Retrieve the "good role" from the guild, using the role id
                role = DirectCast(channel, SocketGuildChannel).Guild.GetRole(123456789)
            ElseIf reaction.Emote.Name.Equals("?") Then
                'Retrieve the "bad role" from the guild, using the role id
                role = DirectCast(channel, SocketGuildChannel).Guild.GetRole(987654321)
            End If

            'If the role was found within the guild and the user currently has the role assigned, remove the role from the user
            If role IsNot Nothing AndAlso user.Roles.Any(Function(r) r.Id = role.Id) Then Await user.RemoveRoleAsync(role)
        End If
    End Function

【讨论】:

  • @Bego。这与我添加 cmets 的旧代码相同。在该特定行我添加了评论This is not a valid function。如何阅读提供的整个解决方案并尝试理解所说的内容。
  • 当然,我看到“这不是一个有效的功能”,我很累,但在许多失败后我问了原因。我会努力的,RN,希望我能得到它,作为答案。
  • 它现在工作了,但它只是添加角色,当我删除我的反应时,而不是删除我的角色。它只能添加
  • 谢谢,我都修好了。
猜你喜欢
  • 2021-08-04
  • 2019-02-12
  • 2021-08-26
  • 2019-05-31
  • 1970-01-01
  • 2017-05-31
  • 2018-07-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多