当前位置: 首页 > 知识库问答 >
问题:

如何从MySQL数据库中插入和提取数据并将其发送到Discord

吴高畅
2023-03-14

我是一个初学者,我想用Python写一个不和谐的机器人。我已经连接到我的数据库,我从数据库中获取数据,但我不知道如何将数据发送到discord服务器通道。

conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='', db='xp')
cur = conn.cursor()
cur.execute("SELECT dis_id FROM users ")

for row in cur:
    xp = row
    print(xp)

if message.content.startswith('+xp'):
    mbed = discord.Embed(
       title=" Bot Information ",
       description = str(xp),
       colour=(discord.Colour.green())
    )
    channel = client.get_channel(828248385369669642)
    await channel.send(embed=mbed)
    

共有1个答案

张淳
2023-03-14

你的问题不够清楚。我猜你正在尝试获得一个用户拥有的XP数量。如果我错了,纠正我,我会更新答案。

我假设您的表中有一列dis_id和一列XP。如果是这样,这里介绍如何根据用户ID获取XP。

首先,如果您计划执行一个命令,则需要指定一个命令前缀。在定义客户时执行此操作。

client = commands.Bot(command_prefix='+') #"+" is the command prefix. Every message starting with "+" will be a command.

现在让我们创建xp命令,以便当有人在聊天中键入+xp时,将执行该命令。

@client.command() #All commands start with this line called a decorator.
async def xp(ctx, user_id : int): # The command takes in a "context" argument and a "user_id" argument.
    cur.execute(f"SELECT * FROM users WHERE dis_id = {user_id}") # Select all the items for the specified "user_id"

    for row in cur: 
        xp = int(row[0]) # Get the amount of xp the user has
        print(xp)

    mbed = discord.Embed( # Define the embed
       title=" Bot Information ",
       description = str(xp),
       colour=(discord.Colour.green())
    )
    channel = client.get_channel(828248385369669642)
    await channel.send(embed=mbed) #Send the embed to the channel

因此,现在当您想要运行这个命令时,您需要在不和谐聊天中键入+xp user_id。机器人将发送一个包含用户拥有的XP数量的嵌入。

下面是完整的代码

import discord
from discord.ext import commands
import mysql.connector

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

conn = mysql.connector.connect(host='localhost', port=3306, user='root', passwd='', database='xp')
cur = conn.cursor()

@client.command()
async def xp(ctx, user_id : int):
    cur.execute(f"SELECT * FROM users WHERE dis_id = {user_id}") 

    for row in cur:
        xp = int(row[0])
        print(xp)

    mbed = discord.Embed(
       title=" Bot Information ",
       description = str(xp),
       colour=(discord.Colour.green())
    )
    channel = client.get_channel(828248385369669642)
    await channel.send(embed=mbed)
 类似资料:
  • 问题内容: 我有这个小点击计数器。我想将每次单击都包含在mysql表中。有人可以帮忙吗? 万一有人想看看我做了什么: 这是phpfile.php,出于测试目的,将数据写入txt文件 问题答案: 您的问题中定义的JavaScript不能直接与MySql一起使用。这是因为它不在同一台计算机上运行。 JavaScript在客户端(在浏览器中)运行,并且数据库通常在服务器端存在。您可能需要使用中间服务器端

  • 问题内容: 我想在我的MySQL数据库中插入整数188和90,但以下代码不起作用: 为什么不起作用? 问题答案: 编辑 为我工作: 在MySQL表;

  • 问题内容: 我有一个应用程序,我想从excel读取数据,将其插入数据库,然后为特定用户生成pdf报告。我进行了很多搜索,但没有具体说明这两种情况。 问题答案: 使用PHPExcel库读取Excel文件并将数据传输到数据库中 一切都变得非常取决于您的数据库以及如何在其中构造数据

  • 问题内容: 我需要从XML文件中获取数据并将其存储到MySQL数据库中。我正在考虑使用SAX解析器来解析数据,但是我不确定如何将数据有效地存储到数据库中,我正在考虑使用JDBC和Hibernate等少数技术,但是我想问一问有效的处理方式它? 注意:此处的编程语言是Java。 问题答案: 您可以使用Castor witch是一个开放源数据绑定框架,用于将数据从XML移动到Java编程语言对象以及从J

  • 问题内容: 我正在尝试使用angularjs从前端向mysql db插入数据。 但是,即使没有错误消息,它也不会插入数据库 。以下是我使用的代码。 index.html script.js View1.html 以下是我的php文件 insert.php 我知道我在这里做一些愚蠢的事情。我今天才刚刚开始学习angularjs。当我尝试将纯HTML的php代码插入db时,它的工作原理非常完美。希望有

  • 问题内容: 如标题所示,我需要将一些数据(从数据库中获取)放入Excel工作表中,然后将其发送到客户端,以便用户可以保存,打开或取消操作。 我看到了一些与此有关的文章,最近的是:如何让用户下载文件?(Java,MVC,Excel,POI)。参考史蒂文斯提供的链接,我尝试了以下代码: 首先这里没有定义。其次,我无法正确理解代码的工作方式。 我还找到了此链接:http : //www.roseindi