安装discord2
# 如果有旧discordpy 删除
pip uninstall discord.py
# 安装新库
python3 -m pip install py-cord
用的类调用
main.py
import discord
from discord.ext import commands
intents = discord.Intents.all()
bot = commands.Bot(command_prefix='!', intents=intents)
# 申请token
token = '*****'
if __name__ == '__main__':
bot.load_extension('CMDS')
bot.run(token)
CMDS.py
import asyncio
import discord
from discord.ext import commands
# 单独服务器,服务工会id,只适合单独工会
guild_id = 1232321121
class CogExtension(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
self.guild = None
@commands.Cog.listener()
async def on_ready(self):
self.guild = get_guild(bot)
print('on_ready')
def get_guild(bot):
guild = None
for i in bot.guilds:
# 服务器id
if i.id == guild_id:
guild = i
continue
return guild
class CMD(CogExtension):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
async def interaction_button():
await self.bot.wait_until_ready() # 等待bot就绪
await asyncio.sleep(1)
while not self.bot.is_closed():
if self.guild:
interaction = await self.bot.wait_for("interaction")
print("interaction", interaction.custom_id)
if interaction.custom_id == 'button-ephemeral-true':
await interaction.response.send_message("button-ephemeral-true", ephemeral=True)
elif interaction.custom_id == 'button-ephemeral-false':
await interaction.response.send_message("button-ephemeral-false", ephemeral=False)
# else:
# await interaction.edit_original_message(content='edit')
self.bot.loop.create_task(interaction_button())
# 错误处理
@commands.Cog.listener()
async def on_command_error(self, ctx, error):
pass
@commands.command(name='ping')
async def ping(self, ctx):
await ctx.send("pong")
# 发送带有按钮的消息
@commands.command(name='send-message-button-true')
@commands.check(is_team)
async def send_msg_button_true(self, ctx):
item = discord.ui.Button(custom_id='button-ephemeral-true', label='button-ephemeral-true')
view = discord.ui.View()
view.add_item(item)
try:
await ctx.send("team pong", view=view)
except Exception as e:
print(e)
# 发送带有按钮的消息
@commands.command(name='send-message-button-false')
@commands.check(is_team)
async def send_msg_button_false(self, ctx):
item = discord.ui.Button(custom_id='button-ephemeral-false', label='button-ephemeral-false')
view = discord.ui.View()
view.add_item(item)
try:
await ctx.send("team pong", view=view)
except Exception as e:
print(e)
# 斜杠命令 /
# guild_ids 匹配的工会id列表 description 简介 name 指令名称,如果没有就是函数名
@commands.slash_command(name='hello-respond', guild_ids=[guild_id], description='hello')
async def hello_respond(self, ctx, member: discord.Member, age: int):
# 类似回复指令
await ctx.respond(f"Hello respond, {member}, age{age}!")
@commands.command(name='hello-send', guild_ids=[guild_id], description='hello')
async def hello_send(self, ctx, member: discord.Member, age: int):
# 类似回复指令
await ctx.send(f"Hello respond, {member}, age{age}!")
调用 !send-message-button-true 指令,点击按钮消息只能本人看到
调用指令!send-message-button-false,返回信息公共可以看到
还有定时任务等功能有机会在写
测试了一下 这段代码有点问题·interaction_button·把这个函数删了,改成对象方法
class CMD(CogExtension):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# async def interaction_button():
# await self.bot.wait_until_ready() # 等待bot就绪
# await asyncio.sleep(1)
# while not self.bot.is_closed():
# if self.guild:
#
#
# self.bot.loop.create_task(interaction_button())
@commands.Cog.listener()
async def on_interaction(self, interaction):
pass # 之前方法里的逻辑