如何更改我的代码以添加删除频道中所有消息的命令?

时间:2021-04-15 13:10:17

标签: python discord

这是我当前的代码。我想知道如何添加一个可以删除频道内所有消息的命令。为了添加一些上下文,我正在为我和我的朋友制作这个机器人,而且我们经常发现自己处于垃圾邮件战争中。但是,当我尝试制作类似的东西时,我不小心混淆了客户端命令和客户端事件,这只会使整个代码变得无用。我试图避免重新格式化整个机器人,那么如何使用消息功能删除当前频道中的每条消息?


import discord
import os
import requests
import json
import random
from replit import db

client = discord.Client()

sad_words = ["sad", "depressed", "unhappy", "angry", "miserable"]

starter_encouragements = [
  "Cheer up!",
  "Hang in there.",
  "You are a great person / bot!"
]

bad_words = ["shit", "fuck", "bitch", "FUCK", "wtf"]

anti_bad_word = [
  "Hey, don't say that ~~when you can be heard~~.",
  "Calm down.",
  "*placating gestures*",
]

jasper_summons = [
  "jasper",
  "Jasper",
  "JASPER"
  "J.A.S.P.E.R"
]

if "responding" not in db.keys():
  db["responding"] = True

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)

def update_encouragements(encouraging_message):
  if "encouragements" in db.keys():
    encouragements = db["encouragements"]
    encouragements.append(encouraging_message)
    db["encouragements"] = encouragements
  else:
    db["encouragements"] = [encouraging_message]

def delete_encouragment(index):
  encouragements = db["encouragements"]
  if len(encouragements) > index:
    del encouragements[index]
  db["encouragements"] = encouragements

def update_antibad(anti_bad):
  if "antibad" in db.keys():
    antibad = db["antibad"]
    antibad.append(anti_bad)
    db["antibad"] = antibad
  else:
    db["antibad"] = [anti_bad]

def delete_antibad(index):
  antibad = db["antibad"]
  if len(antibad) > index:
    del antibad[index]
  db["antibad"] = antibad

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

@client.event
async def on_message(message):
  if message.author == client.user:
    return

  msg = message.content

  if msg.startswith("j/inspire"):
    quote = get_quote()
    await message.channel.send(quote)

  if db["responding"]:
    niceoptions = starter_encouragements
    if "encouragements" in db.keys():
      niceoptions = niceoptions + db["encouragements"]
    antibadoptions = anti_bad_word
    if "antibad" in db.keys():
      antibadoptions = antibadoptions + db["antibad"]

    if any(word in msg for word in sad_words):
      await message.channel.send(random.choice(niceoptions))

    if any(word in msg for word in bad_words):
      await message.channel.send(random.choice(antibadoptions))

  if msg.startswith("j/newnice"):
    encouraging_message = msg.split("j/newnice ",1)[1]
    update_encouragements(encouraging_message)
    await message.channel.send("New encouraging message added.")

  if msg.startswith("j/newanti"):
    anti_bad = msg.split("j/newanti ",1)[1]
    update_antibad(anti_bad)
    await message.channel.send("New anti-bad message added.")


  if msg.startswith("j/delnice"):
    encouragements = []
    if "encouragements" in db.keys():
      index = int(msg.split("j/delnice",1)[1])
      delete_encouragment(index)
      encouragements = db["encouragements"]
    await message.channel.send(encouragements)

  if msg.startswith("j/delanti"):
    antibad = []
    if "antibad" in db.keys():
      index = int(msg.split("j/delanti",1)[1])
      delete_antibad(index)
      antibad = db["antibad"]
    await message.channel.send(antibad)

  if msg.startswith("j/listnice"):
    encouragements = []
    if "encouragements" in db.keys():
      encouragements = db["encouragements"]
    await message.channel.send(encouragements)

  if msg.startswith("j/listanti"):
    antibad = []
    if "antibad" in db.keys():
      antibad = db["antibad"]
    await message.channel.send(antibad)
    
  if msg.startswith("j/responding"):
    value = msg.split("j/responding ",1)[1]

    if value.lower() == "true":
      db["responding"] = True
      await message.channel.send("Responding is on.")
    else:
      db["responding"] = False
      await message.channel.send("Responding is off.")

client.run('TOKEN (sorry, can't reveal this.)')```

1 个答案:

答案 0 :(得分:0)

有一个方法叫做 ctx.channel.purge()。只需确保您的机器人具有适当的权限即可。

@client.command()
async def foo(ctx):
    await ctx.channel.purge()