Pythonを使ってボットを作る


PythonとMySQLを使用してデータベース接続を作成した後、今私のボットのコマンドを開始する時間です.
各種コマンドの作成手順
1 .カスタムの作成Help コマンド.
2 .作成Helper and Member コマンドポイントを追加します.
3 .作成points 特定のユーザーのポイントを表示するコマンドです.
4 .作成Helper and Member リーダーコマンド.
最初にダウンロードし、必要なすべてのライブラリをインポートします.
discord.py              1.5.1
prettytable             2.0.0
mysql-connector         2.2.9
mysql-connector-python  8.0.21
json
だから、不和はそれ自身のヘルプコマンドです.最初のことは、ヘルプコマンドを削除し、カスタムを作ることでした!
import discord 
import json

from datetime import datetime
from discord.ext import commands
from config_database import *

from prettytable import PrettyTable
from prettytable import from_db_cursor

# Custom Prefix for BOT
bot = commands.Bot(command_prefix="++")

# reading the help commands from `JSON` File 
config_file = open("config.json")
config = json.load(config_file)

# Removing Discord in-build Help command
bot.remove_command("help")
カスタムの作成Help コマンド!
@bot.command(pass_context = True)
async def help(ctx):

    print("!help Command Is Running!")

    embed = discord.Embed(
        title = "Help command list", 
        color=0x8150bc,
        timestamp = datetime.utcnow(),
        description = "Please Follow these commands from any channel!\n\n "
    )

    embed.add_field(name = "1. Member", value = config['Member_add/remove'], inline = False)
    embed.add_field(name = "2. Helper", value = config['Helper_add/remove'], inline = False)
    embed.add_field(name = "3. Leaderboard", value = config['Leaderboard'], inline = False)
    embed.add_field(name = "4. Points", value = config['points'], inline = False)

    embed.set_footer(text = 'Bot Made By Saurabh')
    embed.set_footer(text="Command invoked by {}".format(ctx.message.author.name))
    await ctx.send(embed = embed)
メンバーを作成するのに3週間以上かかり、ポイントの追加と削除のためのヘルパーコマンド😢. データベースにデータを挿入するデータベース機能を追加しました.
すべての必要なパラメータを関数に渡す!
submitted_by(message_id, user_tag, username, user_id, message_Content, point, channel_name)
server(channel_name, channel_id, Server_name)
Given_To(message_id, g_name, g_id, g_tag, has_role)
add_points(g_tag, g_name, message_id, point, has_role)
これらのコマンドを終えた後、私はリーダーボードのためのコマンドを作成し、個々のユーザーのポイントを探しています.
特定のユーザを表示するコマンドPoints
@bot.command(pass_context = True)
async def pts(ctx, username: discord.Member = None):
    print('Command points is running!')

    if username:
        tag = username.discriminator
        member_name = username
        user_point = users_points(tag)

        embed = discord.Embed (
            title = f"{member_name} has {user_point[0]} Points!", 
            color = 0x8150bc,
            timestamp = datetime.utcnow(),
        )
        embed.set_footer(text = 'Bot Made By Saurabh')
        embed.set_footer(text="Command invoked by {}".format(ctx.message.author.name))   
        await ctx.send(embed = embed)

    else:
        await ctx.send("Please add Username to see their points🙈 !!!!!")
このメソッドは、Channel .
リーダーのコマンド
@bot.command(pass_context = True)
async def lb(ctx, command = None):
    print('Command Leaderboard is running!')

    mydb, cursor = get_connection()

    if command == 'helper':

        fetch = '''
            SELECT Username, Total_Points, dense_rank() 
            OVER ( order by Total_points desc ) 
            AS 'rank' FROM points WHERE Is_Helper = 1;
        '''

        cursor.execute(fetch)
        value = from_db_cursor(cursor)

        embed = discord.Embed (
            title = f"Leaderboard for {command}!", 
            color = 0x8150bc,
            timestamp = datetime.utcnow(),
            description = "```

\n"+value.get_string(border=True)+"

```"
        )
        embed.set_footer(text = 'Bot Made By Saurabh')
        embed.set_footer(text="Command invoked by {}".format(ctx.message.author.name))   
        await ctx.send(embed = embed)
        print('Fetching Data for Helper Leaderboard!\n')

    elif command == 'member':

        fetch = '''
            SELECT Username, Total_Points, dense_rank() 
            OVER ( order by Total_points desc ) 
            AS 'rank' FROM points WHERE Is_Helper = 0   ;
        '''

        cursor.execute(fetch)
        value = from_db_cursor(cursor)

        embed = discord.Embed (
            title = f"Leaderboard for {command}!", 
            color = 0x8150bc,
            timestamp = datetime.utcnow(),
            description = "```

\n"+value.get_string(border=True)+"

```"
        )
        embed.set_footer(text = 'Bot Made By Saurabh')
        embed.set_footer(text="Command invoked by {}".format(ctx.message.author.name))   
        await ctx.send(embed = embed)
        print('Fetching Data for Member Leaderboard!\n')

    else:
        await ctx.send('Please add helper/member in your command for displaying the leaderboard!')
これらのメソッドは、helper/member パラメータとして渡されます.else ブロック!
検索後StackOverflow , YouTubeの動画を見て、ついに、私のボットは働いていた🎉🤩 これは、ほぼ1.5ヶ月間かかった😩, 以来、私は非常に不協和ボットを作るために新しいだった.
🖖🏼😃 閉じるこの動画はお気に入りから削除されています.🖖🏼😃