[英]Send Message In Specific Channel In cogs discord.py
我試圖在特定頻道中發送消息...我知道如何正常指定頻道,但我在 cogs 中變得非常困惑我真的無法理解如何這樣做....任何幫助表示贊賞:)
@commands.Cog.listener()
async def on_ready(ctx, channel:discord.Channel=None):
channel = '785465306470547457'
role = discord.utils.get(user.server.roles, name="Orange")
message = await ctx.send_message(channel, "React to me!")
while True:
reaction = await ctx.wait_for_reaction(emoji="🟠", message=message)
await ctx.add_roles(reaction.message.author, role)
錯誤
discord.ext.commands.errors.ExtensionFailed: Extension 'cogs.rolesystem' raised an error: AttributeError: module 'discord' has no attribute 'Channel'
staticmethod
或classmethod
)函數總是將self
作為第一個參數on_ready
不帶任何 argumentsdiscord.TextChannel
而不是discord.Channel
也僅在命令中使用它abc.Messageable
object 不是string
user
, server
也已在1.0.0
版本中重命名為guild
ctx.send
不是ctx.send_message
bot.wait_for
不是ctx.wait_for_reaction
另外,while循環應該做什么?
@commands.Cog.listener()
async def on_ready(self):
"""This will wait for someone to react with `🟠`
and add him a role
Note: This will wait only for one reaction"""
channel_id = 785465306470547457
# Getting the channel
channel = self.bot.get_channel(channel_id)
# Getting the role
role = discord.utils.get(channel.guild.roles, name='Orange')
# Sending the message
message = await channel.send('React to me')
def check(reaction, user):
"""Checks if the reaction message is the one sent before
and if the reaction is `🟠`"""
return reaction.message == message and str(reaction) == '🟠'
# Waiting for the reaction
reaction, user = await self.bot.wait_for('reaction_add', check=check)
# Adding the role
await user.add_roles(role)
注意:除非您啟用默認意圖和意圖,否則此代碼將不起作用。 intents.members
這里是對意圖的介紹。
請閱讀文檔,它真的很有幫助,並在深入了解python和OOP之前了解更多信息discord.py
。
另外,從代碼中,我想您想發送一條 static 消息,當有人做出反應時,為他添加一個角色,為此使用on_raw_reaction_add
事件
參考:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.