discord.py按钮在重置后停止



类按钮处理程序(discord.ui.View(:definit(self(:super((初始化(超时=无(

@discord.ui.button(label="Verify", style=discord.ButtonStyle.primary, custom_id='persistent_view:primary')
async def buttons_verify(self, interaction: discord.Interaction, button: discord.ui.Button):
#interaction.response.send_message("you clicked me")
db = self.client.mongoConnect["zbot"]
collection = db["Verify"]
verify_data = collection.find_one({"_id" : interaction.guild_id})

role = discord.utils.get(interaction.guild.roles, id=verify_data['roles'])
if role not in interaction.user.roles:
await interaction.user.add_roles(role)
await interaction.response.send_message("Welcome to the server", ephemeral=True)
else:
await interaction.response.send_message("Already Verified", ephemeral=True)

类规则(commands.Cog(:

def __init__(self, client: commands.Bot):
self.client = client  
#test the button function this code is confirmed to work in python 2.0
#@app_commands.commands.command(name="button", description="just a button")
#async def button1(self, interaction: Interaction):
#    view = buttonHandler()
#    await interaction.response.send_message("test", view=view)




@app_commands.command(name="rules", description="(Admin only)add rules and make a reaction please please add a role named Member for this to work")
async def self_role(self, interaction: discord.Interaction, channel: str, role: str):
##Gets the mongodb database
db = self.client.mongoConnect["zbot"]
collection = db["Verify"]



## turns the channel into a channel id
role_new = re.sub(r'[^0-9]', '', role)
role_id_int = int(role_new)
channel_new = re.sub(r'[^0-9]', '', channel)
channel_id_int = int(channel_new)
channel_int_new = self.client.get_channel(channel_id_int)

##
await interaction.response.send_message("enter your message")
#message = ''
def check(m):

return m.author == interaction.user and m.channel == interaction.channel
view = buttonHandler(role=role_id_int)
msg = await self.client.wait_for('message',timeout=120.0, check=check)
await channel_int_new.send(msg.content, view=view)
if await collection.find_one({"_id" : interaction.guild_id}) == None:
newData = {"_id" : interaction.guild_id, "roles": role_id_int}
await collection.insert_one(newData)
else:
await collection.replace_one({"_id" : interaction.guild_id, "roles": role_id_int})



每当我重置机器人时,按钮就会停止工作我试图用Raptz/discord.py persistent.py使其持久化,但它不起的作用

说明

由于视图在机器人重新启动时不会自动重新初始化,因此您需要自己进行初始化。这可以通过在机器人程序的setup_hook中调用Bot.add_view来实现。

一些重要注意事项:

为了在重新启动之间保持视图,它需要满足以下条件:

  1. 视图的超时必须设置为无
  2. 视图中的每个项都必须设置一个custom_id

代码

import discord
from discord.ext import commmands

class PersistentView(discord.ui.View):
def __init__(self):
super().__init__(timeout=None)
@discord.ui.button(label='Green', style=discord.ButtonStyle.green, custom_id='persistent_view:green')
async def green(self, interaction: discord.Interaction, button: discord.ui.Button):
await interaction.response.send_message('This is green.', ephemeral=True)

class PersistentViewBot(commands.Bot):
def __init__(self):
intents = discord.Intents.default()
intents.message_content = True
super().__init__(command_prefix=commands.when_mentioned_or('$'), intents=intents)
async def setup_hook(self) -> None:
# Register the persistent view for listening here.
# Note that this does not send the view to any message.
# In order to do this you need to first send a message with the View, which is shown below.
# If you have the message_id you can also pass it as a keyword argument, but for this example
# we don't have one.
self.add_view(PersistentView())
bot = PersistentViewBot()
bot.run("token")

参考

不和。客户端地址查看

代码摘录自不和谐.py github回购示例

最新更新