如何解决用户点击反应时,我无法发布角色
如您所见,如果您删除了发出代码的位置,则当用户单击反应时,它将被评分并以用户名的形式作为响应出现。因为我在程序中编写了此方法并添加了旧版ModuleBase,所以它必须是有限的,所以我必须返回null,因此我总是在所需类的参数中仅将dobavlyu这个问题像素化,但是现在我不能添加超过3个类因为字段ReactionAdded需要3个参数,所以请告诉我该方法如何在内部获取SocketGuild,SocketGuildUser和Yes角色变量,因为ModuleBase
[Command("react")]
public async Task HandleReaction()
{
RestUserMessage message = await Context.Channel.SendMessageAsync("react");
Program.MessageId = message.Id;
}
internal static ulong MessageId { get; set; }
public ITextChannel textChannel;
public SocketGuildUser user;
private async Task OnReactionAdd(Cacheable<IUserMessage,ulong> cache,ISocketMessageChannel channel,SocketReaction reaction)
{
if (reaction.MessageId == Program.MessageId)
{
if (reaction.Emote.Name == "?")
{
ulong roleid = 747992707351183541;
var role = textChannel.Guild.GetRole(roleid); //System.NullReferenceException: "Object reference not set to an instance of an object."
await user.AddRoleAsync(role);
await channel.SendMessageAsync(reaction.User.Value.Username);
}
}
}
解决方法
private async Task OnReactionAdd(Cacheable<IUserMessage,ulong> cache,ISocketMessageChannel channel,SocketReaction reaction)
{
if (reaction.MessageId == Program.MessageId)
{
if (reaction.Emote.Name == "?")
{
ulong roleid = 747992707351183541;
//here can cast the ISocketMessageChannel to an ITextChannel
if (channel is SocketTextChannel textChannel)
{
var role = textChannel.Guild.GetRole(roleid);
var user = textChannel.Guild.GetUser(reaction.UserId);
await user.AddRoleAsync(role);
await textChannel.SendMessageAsync(user.Username);
}
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。