用Python编程Discord机器人-如何使机器人运行得更快?

用Python编程Discord机器人-如何使机器人运行得更快?,python,discord.py,Python,Discord.py,我注意到每当命令被触发时,机器人通常需要几秒钟的时间来响应。有没有办法提高机器人的整体速度?我是编程新手,因此任何有见识的人都将不胜感激。以下是我的代码(如果有帮助): import discord import os import random import praw from keep_alive import keep_alive from discord.ext import commands from discord.ext.commands import Bot import tim

我注意到每当命令被触发时,机器人通常需要几秒钟的时间来响应。有没有办法提高机器人的整体速度?我是编程新手,因此任何有见识的人都将不胜感激。以下是我的代码(如果有帮助):

import discord
import os
import random
import praw
from keep_alive import keep_alive
from discord.ext import commands
from discord.ext.commands import Bot
import time


client = commands.Bot(command_prefix='.')


sec_triggers = ['just a sec', 'Just a sec', 'just a second', 'Just a second', 'one sec', 'one second', 'One sec', 'One second']
monke_triggers = ['monke', 'Monke', 'Monkey', 'monkey']
hello_triggers = ['hello there', 'Hello there', 'hello There', 'Hello There']
f_triggers = ['f in the chat', 'F in the chat', 'f in the Chat', 'F in the Chat']
colors = [0xff0000, 0xff3300, 0xff6600, 0xff9900, 0xffcc00, 0xffff00, 0xccff00, 0x99ff00, 0x66ff00, 0x33ff00, 0x00ff00, 0x00ff33, 0x00ff66, 0x00ff99, 0x00ffcc, 0x00ffff, 0x00ccff, 0x0099ff, 0x0066ff, 0x0033ff, 0x0000ff, 0x3300ff, 0x6600ff, 0x9900ff, 0xcc00ff, 0xff00ff, 0xff00cc, 0xff0099, 0xff0066, 0xff0033]
client.remove_command('help')

@client.event
async def on_ready():
  print('We have logged in as {0.user}'.format(client))

  await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="you, Wazowski. Always Watching. Always."))



@client.event
async def on_message(message):
  if message.author == client.user:
   return
  
  msg = message.content
  
  if any(word in msg for word in monke_triggers):
   await message.channel.send(file=discord.File('Reject Humanity, Return to Monke.jpg'))
  
  if any(word in msg for word in sec_triggers):
   time. sleep(1)
   await message.channel.send("It's been one second")
  
  if any(word in msg for word in hello_triggers):
    await message.channel.send(file=discord.File('General_Kenobi.gif'))
  
  if any(word in msg for word in f_triggers):
    mention = message.author.name
    await message.channel.send(f"{mention} had paid their respects.")

  if message.content.lower() == 'f' or message.content.lower() == 'F':
    mention = message.author.name
    await message.channel.send(f"{mention} had paid their respects.")
  
  await client.process_commands(message)


@client.command()
async def catjam(ctx, *, text):
    message = f"{text}"
    new_message = ""
    for char in message:
      new_message += f"<a:catjam:800476635655962644>{char}"
    new_message += "<a:catjam:800476635655962644>"
    await ctx.send(new_message)
    await ctx.message.delete()

@client.command()
async def echo(ctx, *, text):
  await ctx.send(f"{text}")
  await ctx.message.delete()

@client.command()
@commands.has_permissions(kick_members=True)
async def kick(ctx, member: discord.Member, *, text):
    reason = f"{text}"
    mention = ctx.message.author.name
    pfp = member.avatar_url
    em = discord.Embed(title = f"{member} has been kicked.", color = random.choice(colors))
    em.add_field(name="Reason:", value=reason)
    em.add_field(name="Responsible User:", value=mention)
    em.set_thumbnail(url=(pfp))
    await member.kick(reason=reason)
    await ctx.send(embed=em)

@client.command()
@commands.has_permissions(ban_members=True)
async def ban(ctx, member: discord.Member, *, text):
    reason = f"{text}"
    mention = ctx.message.author.name
    pfp = member.avatar_url
    em = discord.Embed(title = f"{member} has been banned.", description= f"__Reason:__ {reason} __Responsible moderator:__ {mention}", color = random.choice(colors))
    em.set_thumbnail(url=(pfp))
    await member.ban(reason=reason)
    await ctx.send(embed=em)


@client.command()
async def comic(ctx):
    subreddit = reddit.subreddit("comic")
    all_subs = []
    hot = subreddit.hot(limit = 100)

    for submission in hot:
      all_subs.append(submission)
  
    random_sub = random.choice(all_subs)
    name = random_sub.title
    url = random_sub.url
    em = discord.Embed(title = name, color = random.choice(colors))

    em.set_image(url = url)
    await ctx.send(embed = em)

@client.command()
async def joke(ctx):
    subreddit = reddit.subreddit("cleanjokes")
    all_subs = []
    hot = subreddit.hot(limit = 100)

    for submission in hot:
      all_subs.append(submission)
  
    random_sub = random.choice(all_subs)
    name = random_sub.title
    url = random_sub.url
    text = random_sub.selftext
    em = discord.Embed(title = name, color = random.choice(colors), description = text)

    await ctx.send(embed = em)


@client.command()
async def meme(ctx):
    subreddit = reddit.subreddit("cleanmemes")
    all_subs = []
    hot = subreddit.hot(limit = 100)

    for submission in hot:
       all_subs.append(submission)
  
    random_sub = random.choice(all_subs)
    name = random_sub.title
    url = random_sub.url
    em = discord.Embed(title = name, color = random.choice(colors))

    em.set_image(url = url)
    await ctx.send(embed = em)

@client.command()
async def cat(ctx):
    subreddit = reddit.subreddit("catpictures")
    all_subs = []
    hot = subreddit.hot(limit = 100)

    for submission in hot:
       all_subs.append(submission)
  
    random_sub = random.choice(all_subs)
    name = random_sub.title
    url = random_sub.url
    em = discord.Embed(title = name, color = random.choice(colors))

    em.set_image(url = url)
    await ctx.send(embed = em)

@client.command()
async def dog(ctx):
    subreddit = reddit.subreddit("dogpictures")
    all_subs = []
    hot = subreddit.hot(limit = 100)

    for submission in hot:
       all_subs.append(submission)
  
    random_sub = random.choice(all_subs)
    name = random_sub.title
    url = random_sub.url
    em = discord.Embed(title = name, color = random.choice(colors))

    em.set_image(url = url)
    await ctx.send(embed = em)

@client.command()
async def help(ctx):
  em = discord.Embed(color = random.choice(colors))
  em.add_field(name='General Commands', value='__help__- Displays this message', inline=True)
  await ctx.send(embed = em)


@client.command()
async def server(ctx):
  server = ctx.message.guild

  roles = str(len(server.roles))
  emojis = str(len(server.emojis))
  channels = str(len(server.channels))

  embeded = discord.Embed(title=server.name, description='Server Info', color=random.choice(colors))
  embeded.set_thumbnail(url=server.icon_url)
  embeded.add_field(name="Created on:", value=server.created_at.strftime('%d %B %Y at %H:%M UTC+3'), inline=False)
  embeded.add_field(name="Server ID:", value=server.id, inline=False)
  embeded.add_field(name="Users on server:", value=server.member_count, inline=True)
  embeded.add_field(name="Server owner:", value=server.owner, inline=True)


  embeded.add_field(name="Server Region:", value=server.region, inline=True)
  embeded.add_field(name="Verification Level:", value=server.verification_level, inline=True)

  embeded.add_field(name="Role Count:", value=roles, inline=True)
  embeded.add_field(name="Emoji Count:", value=emojis, inline=True)
  embeded.add_field(name="Channel Count:", value=channels, inline=True)

  await ctx.send(embed=embeded) 


keep_alive()

client.run(os.getenv('TOKEN'))
导入不一致
导入操作系统
随机输入
进口婴儿车
从keep_alive导入keep_alive
从discord.ext导入命令
从discord.ext.commands导入Bot
导入时间
client=commands.Bot(命令前缀='。)
sec_triggers=[‘仅一秒’、‘仅一秒’、‘仅一秒’、‘仅一秒’、‘一秒’、‘一秒’、‘一秒’]
monke_触发器=['monke','monke','monke','Monkey','Monkey']
hello_触发器=['hello there','hello there','hello there','hello there','hello there']
f_triggers=['f在聊天中,'f在聊天中,'f在聊天中,'f在聊天中']
颜色=[0xff0000,0xff3300,0xff6600,0xff9900,0xffcc00,0xffff00,0xccff00,0x99ff00,0x66ff00,0x33ff00,0x00ff00,0x00ff33,0x00ff66,0x00ff99,0x00ffcc,0x00ccff,0x0099ff,0x0066ff,0x0033ff,0x0000ff,0x3300ff,0x6600ff,0x9900ff,0xff00ff,0xff00cc,0xff0099,0x0063]
client.remove_命令(“帮助”)
@客户端事件
_ready()上的异步定义:
打印('我们已以{0.user}的身份登录。格式(客户端))
等待客户。更改状态(activity=discord.activity(type=discord.ActivityType.watching,name=“you,Wazowski.Always watching.Always.”)
@客户端事件
异步def on_消息(消息):
如果message.author==client.user:
回来
msg=message.content
如果有(消息中的单词对应monke_触发器中的单词):
等待message.channel.send(file=discord.file('Reject Humanity,Return to Monke.jpg'))
如果有(msg中的单词对应sec_触发器中的单词):
时间睡眠(1)
等待message.channel.send(“已经过了一秒钟”)
如果有(消息中的单词对应hello_触发器中的单词):
等待message.channel.send(file=discord.file('General_Kenobi.gif'))
如果有(msg中的单词表示f_触发器中的单词):
提及=message.author.name
wait message.channel.send(f“{提及}已经表达了他们的敬意。”)
如果message.content.lower()
提及=message.author.name
wait message.channel.send(f“{提及}已经表达了他们的敬意。”)
等待客户端处理命令(消息)
@client.command()
异步def catjam(ctx,*,文本):
message=f“{text}”
新建_message=“”
对于消息中的字符:
新消息+=f“{char}”
新消息+=“”
等待ctx.发送(新消息)
等待ctx.message.delete()
@client.command()
异步def回显(ctx,*,文本):
等待ctx.send(f“{text}”)
等待ctx.message.delete()
@client.command()
@commands.has_权限(kick_members=True)
异步def kick(ctx,成员:discord.member,*,text):
原因=f“{text}”
提及=ctx.message.author.name
pfp=member.avatar\u url
em=discord.Embed(title=f“{member}已被踢出。”,color=random.choice(colors))
em.add_字段(name=“Reason:,value=Reason)
em.add_字段(name=“责任用户:”,value=提及)
em.set_缩略图(url=(pfp))
等待成员。踢(原因=原因)
等待ctx.send(嵌入=em)
@client.command()
@commands.has_权限(ban_members=True)
异步定义禁止(ctx,成员:discord.member,*,text):
原因=f“{text}”
提及=ctx.message.author.name
pfp=member.avatar\u url
em=discord.Embed(title=f“{member}已被禁止。”,description=f“{Reason:{Reason}{Reason}”\uuuuu负责的版主:{nity}”,color=random.choice(颜色))
em.set_缩略图(url=(pfp))
等待成员。禁止(原因=原因)
等待ctx.send(嵌入=em)
@client.command()
异步定义(ctx):
subreddit=reddit.subreddit(“喜剧”)
所有_subs=[]
热=子reddit.hot(极限=100)
如欲以即时通讯方式提交:
所有附件(提交)
random\u sub=random.choice(所有\u sub)
名称=随机子标题
url=随机子url
em=discord.Embed(title=name,color=random.choice(colors))
em.set_图像(url=url)
等待ctx.send(嵌入=em)
@client.command()
异步定义(ctx):
subreddit=reddit.subreddit(“干净笑话”)
所有_subs=[]
热=子reddit.hot(极限=100)
如欲以即时通讯方式提交:
所有附件(提交)
random\u sub=random.choice(所有\u sub)
名称=随机子标题
url=随机子url
text=random_sub.selftext
em=discord.Embed(title=name,color=random.choice(colors),description=text)
等待ctx.send(嵌入=em)
@client.command()
异步def内存(ctx):
subreddit=reddit.subreddit(“cleanmemes”)
所有_subs=[]
热=子reddit.hot(极限=100)
如欲以即时通讯方式提交:
所有附件(提交)
random\u sub=random.choice(所有\u sub)
名称=随机子标题
url=随机子url
em=discord.Embed(title=name,color=random.choice(colors))
em.set_图像(url=url)
等待ctx.send(嵌入=em)
@client.command()
异步def cat(ctx):
subreddit=reddit.subreddit(“catpictures”)
所有_subs=[]
热=子reddit.hot(极限=100)
如欲以即时通讯方式提交:
所有附件(提交)
random\u sub=random.choice(所有\u sub)
名称=随机子标题
url=随机子url
em=discord.Embed(title=name,color=random.choice(colors))
em.set_图像(url=url)
等待ctx.send(嵌入=em)
@client.command()
异步def狗(ctx):
subreddit=reddit.subreddit(“狗图片”)
所有_subs=[]
热=子reddit.hot(极限=100)
如欲以即时通讯方式提交:
所有附件(提交)
random\u sub=random.choice(所有\u sub)
名称=随机子标题
url=随机子url
em=discord.Embed(title=name,color=random.choice(colors))
em.set_图像(url=url)
等待ctx.send(嵌入=em)
@client.command()
异步def帮助(ctx):
em=不和谐。嵌入(颜色=随机。选择(颜色))
em.add_字段(name='General Commands',value=''''.'帮助\'-显示此消息',inline=True)