无法分配角色 discord.net
Can't Assign Roles discord.net
我昨天在测试这段代码,它运行得很好,但今天出了点问题,我不知道在哪里。
bot 自动发送消息,添加 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.
我昨天在测试这段代码,它运行得很好,但今天出了点问题,我不知道在哪里。
bot 自动发送消息,添加 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.