我昨天正在测试此代码,它运行得很好,但是今天出现了问题,我不知道哪里。
机器人自动发送消息,增加了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
该代码昨天有效,但我可能已经更改了一点。如果一个用户对新消息做出反应,则什么都不会发生。
找出必须添加的必须
ReactionMessages = msg.Id 'after Await Msg, [single msg track id]
ReactionMessages.Add(msg.Id) 'after Await Msg, [Multi msgs track id] By @Anu6is| Thanks.