【问题标题】:Discord py, the bot sends a message twiceDiscord py,bot 两次发送消息
【发布时间】:2020-11-22 15:58:38
【问题描述】:

我对 StackOverflow 完全陌生,对 python 也很陌生。我已经编写了几天的机器人代码,直到今天晚上它都运行良好。我试图添加一个调平系统(我在这里找到的,但不记得在哪里)。它确实有效,但现在我的机器人在每次命令时都会在聊天中发送两次消息。

这是我的相关代码:

import discord
import datetime
import requests
import random
import json
import time
import asyncio
import discord.ext.commands
from discord import FFmpegPCMAudio
from discord import Game
from discord import Intents
from discord.ext.commands import Bot
from discord.utils import get
from discord.ext import commands

intents = Intents.all()
BOT_PREFIX = ("z!", "Z!", "zticko!")
TOKEN = 'REDACTED'

@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')
    activity = discord.Activity(name='Dennis... ;)', type=discord.ActivityType.watching)
    await client.change_presence(activity=activity)

@client.event
async def on_message(message):
    channel = client.get_channel(#the channel ID)
    if 'among us' in message.content or 'Among us' in message.content:
        await channel.send("Glöm bara inte votea ut <@!userid> första rundan, I promise - han är Impostor 8/10 gånger.")
        emoji = client.get_emoji(robinemo)
        await message.add_reaction(emoji)
    await client.process_commands(message)
    #####################################################################
    #                                                                   #
    #               LEVELSYSTEMET BÖRJAR NEDAN                          #
    #                                                                   #
    #####################################################################
    if message.author.bot == False:
        with open('users.json', 'r') as f:
            users = json.load(f)

        await update_data(users, message.author)
        await add_experience(users, message.author, 5)
        await level_up(users, message.author, message)

        with open('users.json', 'w') as f:
            json.dump(users, f)

    await client.process_commands(message)

async def update_data(users, user):
    if not f'{user.id}' in users:
        users[f'{user.id}'] = {}
        users[f'{user.id}']['experience'] = 0
        users[f'{user.id}']['level'] = 1
    print("Uppdaterar en User")

async def add_experience(users, user, exp):
    users[f'{user.id}']['experience'] += exp
    pass

async def level_up(users, user, message):
    with open('levels.json', 'r') as g:
        levels = json.load(g)
    experience = users[f'{user.id}']['experience']
    lvl_start = users[f'{user.id}']['level']
    lvl_end = int(experience ** (1 / 4))
    if lvl_start < lvl_end:
        await message.channel.send(f'{user.mention} har levlat upp till level {lvl_end}! Gz till ding!')
        users[f'{user.id}']['level'] = lvl_end
    pass



#
# Vid ny användare, lägg in för LEVELSYSTEMET
#

@client.event
async def on_member_join(member):
    with open('users.json', 'r') as f:
        users = json.load(f)

    await update_data(users, member)

    with open('users.json', 'w') as f:
        json.dump(users, f)
    
    print("Ny user!")


@client.command(description="Kolla vilken level du ligger på.", brief="Vilken level är du?")
async def level(ctx, member: discord.Member = None):
    if not member:
        id = ctx.message.author.id
        with open('users.json', 'r') as f:
            users = json.load(f)
        lvl = users[str(id)]['level']
        await ctx.send(f'Du är på level {lvl}!')
    else:
        id = member.id
        with open('users.json', 'r') as f:
            users = json.load(f)
        lvl = users[str(id)]['level']
        await ctx.send(f'{member.mention} är på level {lvl}!')
    pass

输出类似于:

机器人 今天 04:17 @User är på 2 级! @User är på 2 级!

而且它只应该发送其中一个。它还运行我拥有的所有其他命令两次。它没有这样做,直到我添加了调平系统。有人可以指出我正确的方向吗?如果这是一个菜鸟的错误,我很抱歉,我正在努力学习和平衡我的医学学习,哈哈......

【问题讨论】:

  • 哦哇,对不起,伙计们。我在 on_message 事件中有两次“await client.process_commands(message)”行。我删除了一个,现在可以使用了!

标签: python discord.py


【解决方案1】:

哦,哇,对不起,伙计们。我在 on_message 事件中有两次“await client.process_commands(message)”行。我删除了一个,现在可以使用了!

【讨论】:

    【解决方案2】:

    你需要做的:

    @client.command()
    async def command(ctx): #u can change the comand if you want
        await ctx.send("enter message here")
        await ctx.send("enter same message here")
    

    【讨论】:

      猜你喜欢
      • 2021-06-17
      • 1970-01-01
      • 1970-01-01
      • 2020-07-30
      • 2020-06-13
      • 2020-12-01
      • 2021-03-02
      • 2021-01-12
      • 2021-06-05
      相关资源
      最近更新 更多