我昨天正在测试此代码,它可以正常工作,但是今天出了点问题,我不知道在哪里。
机器人会自动发送一条消息,添加1条反应,但问题是在进行反应时没有分配角色。
我的代码:
Imports Discord
Imports Discord.WebSocket
Imports Discord.Rest
Dim discord As DiscordSocketClient
Public ReactionMessages As ULong
Public token_ As String = "Token_Hiden"
Public Async Function onMsg(message As SocketMessage) As Task
If message.Source = MessageSource.Bot Then
Dim reaction As SocketReaction
Dim rMessage = CType(Await message.Channel.GetMessageAsync(message.Id), RestUserMessage)
If reaction.Emote.Name.Equals("") AndAlso
reaction.Emote.Name.Equals("") Then
End If
End If
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
If ReactionMessages.Contains(cache.Id) Then
Dim role As IRole = Nothing
If reaction.Emote.Name.Equals("") Then
role = DirectCast(channel, SocketGuildChannel).Guild.Roles.FirstOrDefault(Function(x) x.Name = "one")
ElseIf reaction.Emote.Name.Equals("") Then
role = DirectCast(channel, SocketGuildChannel).Guild.Roles.FirstOrDefault(Function(x) x.Name = "two")
End If
If role IsNot Nothing Then Await DirectCast(reaction.User.Value, SocketGuildUser).AddRoleAsync(role)
End If
End Function
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
If ReactionMessages.Contains(cache.Id) Then
Dim role As IRole = Nothing
Dim user As SocketGuildUser = reaction.User.Value
If reaction.Emote.Name.Equals("") Then
role = DirectCast(channel, SocketGuildChannel).Guild.Roles.FirstOrDefault(Function(x) x.Name = "one")
ElseIf reaction.Emote.Name.Equals("") Then
role = DirectCast(channel, SocketGuildChannel).Guild.Roles.FirstOrDefault(Function(x) x.Name = "two")
End If
If role IsNot Nothing AndAlso user.Roles.Any(Function(r) r.Id = role.Id) Then Await user.RemoveRoleAsync(role)
End If
End Function
Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim builder = New EmbedBuilder
builder.WithTitle("test")
builder.AddField("title!", "not much")
builder.WithThumbnailUrl(discord.CurrentUser.GetAvatarUrl)
builder.WithColor(124, 54, 42)
Dim msgg = Await discord.GetGuild("426511769687556100").GetTextChannel("534344526848851988").SendMessageAsync("", False, builder)
Dim my_emo1 As New Emoji("")
Dim my_emo2 As New Emoji("")
Await msgg.AddReactionAsync(my_emo1)
Await msgg.AddReactionAsync(my_emo2)
End Sub
该代码昨天有效,但我可能对其进行了一些更改。如果用户对新消息做出反应,则不会发生任何事情。
答案 0 :(得分:1)
弄清楚必须添加的内容,
ReactionMessages = msg.Id 'after Await Msg, [single msg track id]
ReactionMessages.Add(msg.Id) 'after Await Msg, [Multi msgs track id] By @Anu6is| Thanks.