import discord
import os
import time
import random
from discord.ext import commands
He_haram = [
"Do they not see the birds controlled in the atmosphere of the sky? none holds them up except Allah. Indeed in that are signs for a people who believe. Quran 16:79",
"So be patient. Indeed, the promise of ALLAH is truth Quran 30:60",
"our lord! Forgive me and my parents, and (all) the believers on the day when the reckoning will be established Quran 14:41",
"And for those who fear Allah, he will make their path easy – Quran Al talak: 4"
]
class quotes(commands.Cog):
def __init__ (self, bot):
self.bot = bot
bot = commands.Bot(command_prefix='!')
@commands.command()
async def on_message(message):
if message.author == bot.user:
return
if message.content.startswith('!quotes'):
await message.channel.send(f" {message.author.name}, {random.choice(He_haram)}")
def setup(bot: commands.Bot):
bot.add_cog(quotes(bot))
- 在主目录下创建一个子文件夹,使用主代码
- 将文件夹命名为"Modules"> 将以下代码添加到您的主bot.py文件中。
for filename in os.listdir('./modules'):
if filename.endswith('.py'):
client.load_extension(f'modules.{filename[:-3]}')
- 在"Modules"中创建一个新的python文件目录中。(例如:moderate .py)
- 将以下起始代码放入其中
from discord.ext import commands
class Moderation(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.Cog.listener()
async def on_ready(self):
print('Moderation module has successfully been initialized.')
- 转到文件末尾,输入以下代码
def setup(bot):
bot.add_cog(Moderation(bot))
- 就像你通常做的那样编写适度文件,除非你想使用 创建命令或事件为事件:
@commands.Cog.listener()
为命令:
@commands.command()
如果您想添加另一个齿轮,只需在"Modules"中创建一个新的.py文件。目录,并始终在其中添加开始代码,并在末尾添加部分。如果您还有其他问题,请告诉我。
@commands.command()
async def on_message(message):
if message.author == bot.user:
return
if message.content.startswith('!quotes'):
await message.channel.send(f" {message.author.name},
{random.choice(He_haram)}")
上面的代码是一个事件,所以你不能使用@commands.command()。
你需要使用:
@commands.Cog.listener()