python - Discord 机器人突然对所有内容做出 3 次响应。未进行任何更改,启动时或执行 cmd 时未显示任何错误
问题描述
我的问题在代码下方。
import discord
import os
from keep_alive import keep_alive
from discord.ext import commands
import random
import requests
import json
import asyncio
from discord.ext.commands import MissingPermissions
bot=commands.Bot(command_prefix = '>')
bot.remove_command("help")
def get_quote():
response = requests.get("https://zenquotes.io/api/random")
json_data = json.loads(response.text)
quote = json_data[0]['q'] + " -" + json_data[0]['a']
return(quote)
@bot.event
async def on_ready():
print('The bot is online')
await bot.change_presence(activity=discord.Game('>help | Bot made by ColeTMK'))
@bot.command()
async def quote(ctx):
quote = get_quote()
embed=discord.Embed(title="Random Quote", description=(quote), color=0x00FFFF)
await ctx.send(embed=embed)
@bot.command()
@commands.has_permissions(manage_messages=True)
async def clear(ctx, amount=5):
await ctx.channel.purge(limit=amount+1)
embed=discord.Embed(title="Clear Messages", description=f'{amount} messages were deleted!', color=0x00FFFF)
await ctx.send(embed=embed)
await asyncio.sleep(3)
await ctx.channel.purge(limit=1)
@clear.error
async def clear_error(ctx, error):
if isinstance(error, MissingPermissions):
await ctx.send(f"{ctx.author.mention}, Sorry, you do not have permission to do that!")
@bot.command()
@commands.has_permissions(ban_members=True)
async def ban(ctx, member: discord.Member, *, reason=None):
await member.ban(reason=reason)
embed=discord.Embed(title="Ban", description=f'User {member} has been banned!', color=0x00FFFF)
await ctx.send(embed=embed)
@ban.error
async def ban_error(ctx, error):
if isinstance(error, MissingPermissions):
await ctx.send(f"{ctx.author.mention}, Sorry, you do not have permission to do that!")
@bot.command()
@commands.has_permissions(kick_members=True)
async def kick(ctx, member: discord.Member, *, reason=None):
await member.kick(reason=reason)
embed=discord.Embed(title="Kick", description=f'User {member} has been kicked!', color=0x00FFFF)
await ctx.send(embed=embed)
@kick.error
async def kick_error(ctx, error):
if isinstance(error, MissingPermissions):
await ctx.send(f"{ctx.author.mention}, Sorry, you do not have permission to do that!")
@bot.command()
async def ping(ctx):
embed=discord.Embed(title="Pong!", description=f'My ping is {round(bot.latency*1000)}ms', color=0x00FFFF)
await ctx.send(embed=embed)
@bot.command()
async def shelp(ctx):
embed=discord.Embed(title="Suici** Hotlines", description="US - 800-273-8255, Canada - 833-456-4566, Mexico - 55-5259-8121, Ireland - 116 123 OR text HELLO to 50808, Australia - 13 11 14 United Kingdom - 01708 765200. Remember that you are LOVED! You can do this, we believe in you! If you dont see your country above, please open a support ticket and we will find the # for you.", color=0x00FFFF)
await ctx.send(embed=embed)
@bot.command()
async def deadchat(ctx):
embed=discord.Embed(description="AYOOO CHAT IS DEAD! SPEAK HUMANS SPEAKKK YOU MUST!", color=0x00FFFF)
await ctx.send(embed=embed)
@bot.command()
@commands.has_permissions(manage_roles=True)
async def tempmute(ctx, member: discord.Member, time=0, reason=None):
if reason == None:
reason = "no reason provided"
if member.id == ctx.author.id:
await ctx.send(f"{ctx.author.mention}, AYOO you can't mute yourself!")
return
role = discord.utils.get(ctx.guild.roles, name="muted")
if role in ctx.guild.roles:
await member.add_roles(role)
embed=discord.Embed(title="Temp. Mute", description=f"Offender: {member.mention} was successfully temp. muted!", color=0x00FFFF)
embed.add_field(name="Duration", value=f'{time} seconds', inline=True)
embed.add_field(name="Reason", value=f'{reason}', inline=True)
embed.add_field(name="Moderator", value=f'{ctx.author.name}', inline=True)
await ctx.send(embed=embed)
await asyncio.sleep(time)
await member.remove_roles(role)
else:
await ctx.send(f"{ctx.author.mention}, Make sure your muted role is called `muted` in all lowercase! Then try again.")
@tempmute.error
async def tempmute_error(ctx, error):
if isinstance(error, MissingPermissions):
await ctx.send(f"{ctx.author.mention}, Sorry, you do not have permission to do that!")
@bot.command()
@commands.has_permissions(manage_roles=True)
async def mute(ctx, member: discord.Member, reason=None):
if member.id == ctx.message.author.id: # If author
await ctx.send("You can't mute yourself")
return
if member:
role = discord.utils.get(ctx.guild.roles, name='muted')
if role in ctx.guild.roles:
await member.add_roles(role)
embed=discord.Embed(title="Muted", description=f"Offender: {member.mention} has been muted until further notice!", color=0x00FFFF)
embed.add_field(name="Reason", value=f'{reason}', inline=True)
embed.add_field(name="Moderator", value=f'{ctx.author.name}', inline=True)
await ctx.send(embed=embed)
else:
await ctx.send(f"{ctx.author.mention}, Make sure your muted role is called `muted` in all lowercase! Then try again.")
@mute.error
async def mute_error(ctx, error):
if isinstance(error, MissingPermissions):
await ctx.send(f"{ctx.author.mention}, Sorry, you do not have permission to do that!")
@mute.error
async def mute_error(ctx, error):
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send(f"{ctx.author.mention} HEY YOU NUGGET! You need to tell me who you want to mute! ")
@bot.command()
@commands.has_permissions(manage_roles=True)
async def unmute(ctx, member: discord.Member,):
if member.id == ctx.author.id:
await ctx.send(f"{ctx.author.mention}, your not even muted LMAOOO")
return
role = discord.utils.get(ctx.guild.roles, name='muted')
await member.remove_roles(role)
embed=discord.Embed(title="Unmute", description=f"{member.mention} was succesfully unmuted!", color=0x00FFFF)
embed.add_field(name="Moderator", value=f'{ctx.author.name}', inline=True)
await ctx.send(embed=embed)
@unmute.error
async def unmute_error(ctx, error):
if isinstance(error, MissingPermissions):
await ctx.send(f"{ctx.author.mention}, Sorry, you do not have permission to do that!")
@bot.command()
async def loved(ctx):
embed=discord.Embed(title="Remember that you are loved! :heart: :heart: :heart:", description="Don't forget that!", color=0x00FFFF)
await ctx.send(embed=embed)
@bot.command()
async def say(ctx, *, text):
message = ctx.message
await message.delete()
await ctx.send(f"{text}")
print(f"{message.author} said {text} in {message.author.guild}")
@bot.listen()
async def on_message(message):
if message.author.id == bot.user.id:
return
msg_content = message.content.lower()
curseWord = ['bad words here']
author = message.author.id
if any(word in msg_content for word in curseWord):
await message.delete()
embed=discord.Embed(title="No No Word", description=f"{message.author.mention}, Hey! Those words arent allowed here!", color=0x00FFFF)
await message.channel.send(embed=embed)
print(f"{message.author.name} said {message.content} in {message.author.guild}")
else:
await bot.process_commands(message)
@bot.listen()
async def on_message(message):
if message.content.startswith(">numbergame"):
channel = message.channel
await channel.send("Guess the number from 0-100 by writing the number in this channel!")
number = random.randint(1,100)
def check(m):
return m.content.isdigit() and m.channel == channel and m.author == message.author
print(number)
while True:
try:
msg = await bot.wait_for('message', timeout=30.0, check=check)
except asyncio.TimeoutError:
return await channel.send(f"{message.author.mention}, You are late to guess!")
guess = int(msg.content)
if guess == number:
return await channel.send(f"Correct answer! {message.author.mention}")
elif guess > number:
await channel.send(f"{message.author.mention}, Your guess was too high!")
elif guess < number:
await channel.send(f"{message.author.mention}, Your guess was too low!")
else:
await bot.process_commands(message)
keep_alive()
bot.run(os.getenv('TOKEN'))
从几分钟前开始,每次我执行命令时,它都会打印 3 次,而不是像往常一样只打印一次。没有粘贴两次或类似的命令。我的问题是,代码中有什么东西可以让机器人做到这一点吗?任何帮助将不胜感激。
解决方案
这个问题已经附加到我身上,不用担心,你的代码不是问题。有时,当您关闭程序时,您的机器人仍然可以运行,然后当您重新启动代码时,机器人帐户上有两个已启动的代码等...要修复它,请在您的机器人页面上重新生成令牌并且不要忘记更新结果是您的代码。
推荐阅读
- javascript - 在 React 中从子组件更改父状态(无 Redux)
- git - 如何使“git difftool --dir-diff”与 vim 一起工作
- javascript - 将组件添加到内容可编辑的 div 后如何添加光标位置
- c# - ASP.NET MVC 外部登录重定向 URi 从 http 到 https
- c# - 无法从 JSON 格式检索正确的值。需要协助
- c - 降序链表显示不按顺序显示
- javascript - 我有一个js出现效果问题
- amazon-web-services - aws runInstance 资源被拒绝显式允许所有资源
- django - Django ValueError:无法配置处理程序“文件”
- reactjs - 在 Laravel/React 项目中找不到 React 代码