首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我的不和不能自动地发挥作用

我的不和不能自动地发挥作用
EN

Stack Overflow用户
提问于 2022-01-16 22:49:08
回答 1查看 32关注 0票数 0

我试图制造一个不和谐的机器人,给一个用户的角色,根据他的联盟的传奇排名,我得到的刮一个网站,提供信息。我在服务器上创建了不同的角色。例如,如果他是青铜,机器人将把用户放在青铜角色。用户需要把$rank 'username'和这项工作很好,但它只是没有给出的角色,我没有任何错误。代码是法文的,但它所涉及的部分是英文的。问题在第57-60行(守则末尾):

代码语言:javascript
复制
import discord
import os
from bs4 import BeautifulSoup
import requests
my_secret = os.environ['TOKEN']


def startbot():
  global rank
  client = discord.Client()

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

  @client.event
  async def on_message(message):
    member = message.author.id
    if message.content.startswith('$rank'):
      stringsplit = message.content.split()
      nmb = len(stringsplit)
      if nmb == 1:
        await message.channel.send("aucun username détecté. Ecrire ($rank USERNAME)")
        return
      else:
        headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 OPR/82.0.4227.58',
        'accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        }
        username = stringsplit[1]
        name = "https://na.op.gg/summoner/userName=" + username
        source = requests.get(name, headers = headers)
        try:
          soup = BeautifulSoup(source.text, "html.parser")
        except:
          await message.channel.send("cette utilisateur n'existe pas.")
          return
        rank = soup.find('div' , class_="TierRank")
        try:
          rank = rank.text
        except:
          await message.channel.send("cette utilisateur n'existe pas.")
          return
        if rank == "Unranked":
          await message.channel.send("cette utilisateur est non classé.")
          return
        rank = rank.split()
        rank = rank[0]
  
    else:
      if message.author == client.user:
        return
      await message.delete()
      return
    

    @client.event
    async def on_message(member):
      role = discord.utils.get(member.server.roles, name=rank)
      await client.add_roles(member, role)
  client.run(my_secret)

startbot()
EN

回答 1

Stack Overflow用户

发布于 2022-01-16 23:05:40

对于前缀命令,您应该使用commands.Bot而不是discord.Client

前缀命令应该这样定义:

代码语言:javascript
复制
from discord.ext import commands
bot = commands.Bot(command_prefix=“=“)

@client.command()
async def cmd_name(ctx,*args):
    …
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70734826

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档