如何停止重复的消息,令牌已更改,但没有运行

How to stop repetitive messages, and the token is changed, but it doesn't run

我今天开始学习 python 并制作了一个 Discord 机器人。我有几个问题:

  1. If message.author ==on_message 中使用,但机器人继续回复自己。
  2. 之后,使用新令牌创建了一个新机器人,但代码无效。

我在这个网站上搜索了很多 Google。我没有找到任何解决方案。可以修改我的代码。一切都来自互联网,所以可能会一团糟。请帮助我。

import discord
import asyncio
from discord.ext import commands

client = discord.Client()


bot = commands.Bot(command_prefix="!")

@bot.event
async def on_ready():
    print('Loggend in Bot: ', bot.user.name)
    print('Bot id: ', bot.user.id)
    print('connection was succesful!')
    print('=' * 30)


@client.event
async def on_message(message) :
    
    if on_message.content.startswith('!의뢰'):
        msg = on_message.channel.content[3:]
        embed = discord.Embed(title = "브리핑", description = msg, color = 0x62c1cc)
        embed.set_thumbnail(url="https://i.imgur.com/UDJYlV3.png")
        embed.set_footer(text="C0de") 
        await on_message.channel.send("새로운 의뢰가 들어왔습니다", embed=embed)
        await client.process_commands(message)


client.run("My bot's token.")

你的代码很乱,但现在应该可以了。我附上了评论,让你知道一切是如何运作的。我认为制作自己的机器人的良好起点是阅读 documentation. Especially Quickstart,它向您展示了一个带有解释的简单示例。

!examplehello看看它是如何工作的。

import discord
import asyncio
from discord.ext import commands

# you created 'client' and 'bot' and used them randomly. Create one and use it for everything:
client = commands.Bot(command_prefix="!") # you can change commands prefix here

@client.event
async def on_ready(): # this will run everytime bot is started
    print('Logged as:', client.user)
    print('ID:', client.user.id)
    print('=' * 30)

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('hello'): # you can change what your bot should react to
        await message.channel.send("Hello! (This is not a command. It will run everytime a user sends a message and it starts with `hello`).")

    await client.process_commands(message)

@client.command()
async def example(ctx): # you can run this command by sending command name and prefix before it, (e.g. !example)
    await ctx.send("Hey! This is an example command.")


client.run("YOUR TOKEN HERE")