Im using the discord.py library and using replit for the bot, but my code doesnt work.
my code:
import os
import discord
from discord.utils import get
client = discord.Client()
#client.event
async def on_ready():
print ("We have logged in as {0.user}".format(client))
#client.event
async def on_message(message):
if message.content == ";":
member = message.author
role = get(member.guild.roles, name="Recruiter")
await member.add_roles(role)
client.run(os.getenv("TOKEN"))
First of all, you're using client object and it is not a bot.
This is the way to create a proper bot (not client):
from discord.ext import commands
bot = commands.Bot(command_prefix="!")
You can't code reactions roles without reading the discord.py docs. It involves lots of stuff.
If you want to code it yourself, the only way is to learn and do.
Related
My discord bot is not responding to "test". There are no error messages, so I am very confused why this is happening.
I just started Python today and I wanted to learn how to make a Discord bot
import discord
client = discord.Client(intents = discord.Intents.default())
#client.event
async def on_ready():
general_channel = client.get_channel(1045161050024185899)
await general_channel.send('Bot Activated By-**GotYoHat** \nMade By **GotYoHat** using **Python**')
#client.event
async def on_message(message):
if message.content == 'test':
await message.channel.send('work')
client.run('the token is right here')
I searched the internet for an answer and I can't find a single answer online
You need to turn on intents.
Try to discord.Intents.all()
I tried creating a discord bot but i have failed alot of times, this is my most recent try...
import discord
from discord.ext.commands import Bot
from discord.ext import commands
print("This might take a few seconds")
print("Please wait...\n\n")
client = discord.Client()
bot = commands.Bot(command_prefix='/')
#client.event
async def on_ready():
activity = discord.Game(name="test", type=3)
await bot.change_presence(status=discord.Status.idle, activity=activity)
print("Bot Online!\n")
print('We have logged in as {0.user}'.format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('secret'):
await message.channel.send('What is the "secret" you speak of?')
if ctx.invoked_subcommand is None:
await message.channel.send('Shh!', delete_after=5)
client.run('XXXXXXXXXXXXXXX')
I cant even get through the statues part. What im trying to do is a bot that has a economy (shop and bank). Can someone please help me
As #Joshua Nixon said you should use commands.Bot instead of discord.Client (docs)
To store all the informations you can use json, sqlite or something like mongoDB as database
can someone tell my why my code is not working?
i want the bot to message me what i type after !test.
import discord
from discord.ext.commands import Bot
from discord.ext import commands
from discord import Game
command_prefix = "!"
client = discord.Client()
bot = commands.Bot(command_prefix='!')
#client.event
async def on_ready():
print('prihlaseno za {0.user}'.format(client))
#bot.command()
async def test(ctx, *, mess):
await ctx.send(mess)
client.run('token')
You cannot mix client with bot. Use one or the other (bot is typically used). Then change your code to fit one of these. For example, if you pick bot, rename #client.event to bot.event and change client.run to bot.run.
You are also needlessly importing the Bot class again when you have already imported it from discord.ext.commands. I'm also not sure what discord.Game is at the end of your imports.
I'm learning how to use the discord.py API and this example code (from here: https://discordpy.readthedocs.io/en/latest/quickstart.html) does not work. I think it has something to do with the async syntax, but I have no idea what could be wrong with it.
Any help would be appreciated!
import discord
client = discord.Client()
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
await message.channel.send('Hello!')
client.run('private token that is filled in in the actual code')
On line 7, “async def on_ready():”, the error is an invalid syntax on async. I’m using 3.8.3
client = commands.Bot(command_prefix = '!') instead of client = commands.Client()
You should import commands too
import discord
from discord.ext import commands
I am looking for a way to allow a user to move him or her self and another user to a different voice channel. I already got the command to work for the author of the message, but I am having trouble finding out a way to move another user in the same message. The idea is that the user would be able to type "n!negotiate [Other User]" and it would move the author and the other user to the Negotiation channel.
I would love some help with how I might be able to do this. The code is provided below excluding the tokens and ids.
Code:
import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
import time
Client = discord.Client() #Initialise Client
client = commands.Bot(command_prefix = "n!") #Initialise client bot and prefix
#client.event
async def on_ready():
print("Logged in as:")
print(client.user.name)
print("ID:")
print(client.user.id)
print("Ready to use!")
#client.event
async def on_message(check): #Bot verification command.
if check.author == client.user:
return
elif check.content.startswith("n!check"):
await client.send_message(check.channel, "Nations Bot is online and well!")
async def on_message(negotiation): #Negotiate command. Allows users to move themselves and other users to the Negotiation voice channel.
if negotiation.author == client.user:
return
elif negotiation.content.startswith("n!negotiate"):
author = negotiation.author
voice_channel = client.get_channel('CHANNELID')
await client.move_member(author, voice_channel)
client.run("TOKEN")
You should use discord.ext.commands. You're importing it, but not actually using any of the features.
from discord.ext import commands
import discord
bot = commands.Bot(command_prefix = "n!") #Initialize bot with prefix
#bot.command(pass_context=True)
async def check(ctx):
await bot.say("Nations Bot is online and well!")
#bot.command(pass_context=True)
async def negotiate(ctx, member: discord.Member):
voice_channel = bot.get_channel('channel_id')
author = ctx.message.author
await bot.move_member(author, voice_channel)
await bot.move_member(member, voice_channel)
bot.run('TOKEN')
Here we use a converter to accept a Member as input. Then we resolve the author of the message from the invocation context and move both Members to the voice channel.