【问题标题】:How to get specific data from discord.py messages如何从 discord.py 消息中获取特定数据
【发布时间】:2021-04-23 12:23:12
【问题描述】:

这是完整的代码

import discord
import os
import requests
from bs4 import BeautifulSoup
import lxml
client = discord.Client()
class GameCheck:
    def __init__(self, name):
            source = requests.get(f"https://www.11v11.com/teams/arsenal/tab/opposingTeams/opposition/{name}")
            soup = BeautifulSoup(source.text, 'lxml')
            games = soup.find_all(class_="result-status")
            allgames = []

            for game in games:
                allgames.append(game.text)
            self.last5 = allgames[-5:]
  
@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
    if message.content.startswith('!history'):
      team = message.content
      ars = GameCheck(team)
      await message.channel.send(f"`Our last 5 games with this club: {ars.last5}`")
client.run("Token")

我的问题在

if message.content.startswith('!history'):
          team = message.content
          ars = GameCheck(team)
          await message.channel.send(f"`Our last 5 games with this club: {ars.last5}`")

我想拆分以 !history 开头的不和谐消息,例如 "!history Leicester City/" 将 "Leicester City/" 放入变量 team 中,然后对其运行 GameCheck。当我运行这段代码时,它不会给我一个错误,但无论我输入什么“团队”,它都会一直给我相同的输出。

【问题讨论】:

    标签: python python-3.x python-requests discord.py


    【解决方案1】:

    您实际上是作为反对队传递整个消息,生成的 url 将是“https://www.11v11.com/teams/arsenal/tab/opposingTeams/opposition/!history%20Leicester%20City/”你展示的案例。我们只需从团队名称中删除调用命令(删除!历史)

    team = " ".join(message.content.split()[1:]) #!history is the first word
    

    注意:requestsblocking library。我建议使用那里的答案之一或使用 aiohttp 之类的东西。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-24
      • 2019-04-21
      • 2021-07-30
      • 2020-07-27
      • 2021-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多