import discord
import os
import requests
import json
import random
from random import randint
from replit import db
from discord.ext import commands
client = discord.Client()
bot = commands.Bot(command_prefix='.')
@client.event
async def on_ready():
print(f'{client.user.name} működik!')
await client.change_presence(activity=discord.Game(name='egy bot vagyok'))
@client.event
async def on_member_join(member):
await member.create_dm()
await member.dm_channel.send(
f' {member.name}, itt van!'
)
@bot.command()
async def test(ctx, arg):
await ctx.send(arg)
print(f'haha')
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('ping'):
await message.channel.send("pong")
if message.content.startswith("shrug"):
await message.channel.send('¯\_(ツ)_/¯')
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('.pic'):
await message.channel.send(file=discord.File('combo-0.png'))
token = os.environ.get("secret")
client.run(token)
我是不和谐机器人编程的初学者 这是我的不和谐机器人的全部代码,除了一个命令外,所有命令都不起作用
te .pic 命令
直到现在所有命令都有效,我不知道为什么它们不起作用
如果我能得到一些帮助,我会很高兴 :D (抱歉我的英语不好,英语不是我的第一语言)
答案 0 :(得分:1)
您不能有多个 on_message
事件,只会注册最后一个。你必须把它们合二为一
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('ping'):
await message.channel.send("pong")
if message.content.startswith("shrug"):
await message.channel.send('¯\_(ツ)_/¯')
if message.content.startswith('.pic'):
await message.channel.send(file=discord.File('combo-0.png'))
答案 1 :(得分:0)
您不能像这样混淆 discord.Client()
和 commands.Bot
客户端。
commands.Bot
是 discord.Client
的子类,因此您应该能够使用 discord.Client()
和 commands.Bot()
bot = commands.Bot('!')
@bot.event
async def on_ready():
print('ready')
@bot.command()
async def ping(ctx):
await ctx.send('pong')
有关重写 discord.ext.commands
的帮助,互联网上有大量教程。