Discord机器人运行命令两次Discord.py



这是主bot和一个cog,所有cog中的所有命令在每次调用时都会运行两次,我知道只有一个bot实例在运行,没有重复的命令。我甚至更换了文本编辑器,让多个人查看我的代码,但他们无法找到任何需要的帮助,谢谢。

主Bot称为Main_Bot.py

#imports
from cogs.database import database
import discord
import time
import random
from discord.abc import GuildChannel
from discord.embeds import Embed
from discord.ext import commands
import asyncio
import os
import json
import time
import requests
from bs4 import BeautifulSoup
from discord.ext import tasks
os.chdir(os.getcwd())


intents = discord.Intents.default()
intents.members = True 
client = commands.Bot(command_prefix="-",intents=intents)
client.remove_command("help")
client.load_extension("cogs.moderation")
client.load_extension("cogs.fun")
client.load_extension("cogs.events")
client.load_extension("cogs.eco")
client.load_extension("cogs.help")
print("loaded")


async def newServer(ctx,guild):
role = discord.utils.get(guild.roles,name = "Muted")

@client.event
async def on_ready():
#when the bot powers on
print("Ready")
print(discord.__version__)
await client.change_presence(status=discord.Status.online, activity=discord.Game('Type -help'))
clearTXT.start()
DailyTimer.start()
muteTime.start()


@tasks.loop(seconds=10)
async def clearTXT():
with open("spam_detect.txt", "r+") as file:
file.truncate(0)
@tasks.loop(seconds=60)
async def DailyTimer():
users = await database.get_bank_data()

for i in users:

user = await client.fetch_user(i)
daily_timer = users[str(user.id)]["daily_timer"]
if daily_timer <= 0:
users[str(user.id)]["daily"] = True
else:
users[str(user.id)]["daily_timer"] -= 10
@tasks.loop(seconds=60)
async def muteTime():
users = await database.get_muted_data()
mod_chat = client.get_channel(778671755115888670)
print("muted Time")
for i in users:
print("Looped")
user = await client.fetch_user(i)
print(user)
daily_timer = users[str(user.id)]["time"]
print(daily_timer)
if daily_timer > 0:
users[str(i)]["time"] -= 60
print("Munis 60")
with open("muted_players.json", "w") as f:
json.dump(users,f,indent=2)  


else:

print("unmuted")
getGuild = users[str(user.id)]["server"]
guild = client.get_guild(getGuild)
member = guild.get_member(user.id)
mute_role=discord.utils.get(guild.roles, name="Muted")
await member.remove_roles(mute_role)
#  await deleteMuted(user)  
with open("muted_players.json", "w") as f:
users.pop(str(user.id))
json.dump(users,f,indent=2) 
embed = discord.Embed(description= f"✅ **{member.display_name} mute has run out and is now unmuted**", color=discord.Color.green())
await mod_chat.send(embed=embed)

#run client server
client.run('' )

一个叫fun.py 的Cog

import discord
import asyncio
from discord import client
from discord.ext import commands
import requests
from bs4 import BeautifulSoup
import random
class fun(commands.Cog):
def __init__(self, client):
self.client = client
@commands.Cog.listener()
async def on_ready(self):
print("Fun Cog Loaded")

@commands.command(name="test")
async def test(self,ctx):
await ctx.send("this is a test")

@commands.command(name="embed")
@commands.has_permissions(manage_messages=True)
async def embed(self,ctx,channel:discord.TextChannel,title, * ,words_embed):
person = ctx.author.display_name
words = words_embed
print(words)
em = discord.Embed(title = title, color = discord.Color.dark_red())
em.description = words
#  em.add_field(name=person,value=words)
# em.set_footer(text = person)
await channel.send(embed = em)
await ctx.message.delete()

@commands.command(name="message")
@commands.has_permissions(manage_messages=True)
async def embed(self,ctx,channel:discord.TextChannel, * words_embed):
words = ' '.join([str(words) for words in words_embed]) 
print(words)
await channel.send(words)
await ctx.message.delete()
@commands.command(name='wiki')
async def wikipedia(self,ctx):
a = "https://en.wikipedia.org/wiki/Special:Random"
u = requests.get(a)
soup = BeautifulSoup(u.content, 'html.parser')
title = soup.find(class_ = "firstHeading").text
title = title.replace(" ", "_")
url = 'https://en.wikipedia.org/wiki/%s' %title
await ctx.send(f"""Here is your random Wikipedia article! 
{title} - {url}""")

@commands.command(name="flip")
async def flip(self,ctx):
results = random.randrange(0,2)
print(results)
if results == 1:
em = discord.Embed(title = f"The coin landed on Heads!", color = discord.Color.dark_blue())
await ctx.send(embed = em)
elif results == 0:
em = discord.Embed(title = f"The coin landed on Tails!", color = discord.Color.dark_blue())
await ctx.send(embed = em)
def setup(client):
client.add_cog(fun(client))

这篇reddit帖子提供了解决方案:

如果有

@commands.Cog.listener()
async def on_message(..):

函数在您加载的任何其他cogs扩展中,删除线路

await self.client.process_commands(message)

从它的身体。在cogs加载的扩展文件中不需要它,只在主文件中需要它。

最新更新