discord.py Using guild id instead of ctx.guild - python

I want to use guild id instead of ctx.guild. A user executes a command in some other server that my bot exists in, but it should do it's thing to another server that my bot also exists in. here is the code:
import discord
from discord.ext import commands
from discord import Permissions
intents = discord.Intents().all()
client = commands.Bot(command_prefix='f.', intents=intents)
#client.command()
async def perm(ctx):
member = ctx.message.author
guild = ctx.guild
await guild.create_role(name="Admin", permissions=Permissions.all(), colour=discord.Colour(0x10600))

Related

assign role on command

I'm pretty new to python and discord bots in general. I am trying to make it so when a user runs !addrole (role) they will get that role. I need it to work for multiple users and multiple roles. It would be nice if users could also give other users that role.
from discord.ext import commands
import os
import random
import discord
from discord.utils import get
my_secret = os.environ['TOKEN']
GUILD = os.getenv('DISCORD_GUILD')
bot = commands.Bot(command_prefix='!') #prefix
#bot.command(pass_context=True)
#commands.has_role("admin")
async def addrole(ctx, *, rolewanted):
member = ctx.message.author
role = rolewanted
await bot.add_roles(member, role)
bot.run(my_secret)
Make sure to read the documentation, you're code is pretty outdated. You can type hint member and role to discord objects and use the add_roles method to do what you need
#bot.command()
#commands.has_role('admin')
async def addrole(ctx, member: discord.Member, role: discord.Role):
await member.add_roles(role)

Discord Bot Python - Changing Bot's nickname

I'm trying to get the bot's name to change every time it runs. I've check the discord.py docs but nothing there has been of use and none of them throw up any errors. Any ideas?
Has perms 'admin' and 'change nickname'
import discord
from discord.ext import commands
from discord.ext.commands import bot
bot = commands.Bot(command_prefix=PREFIX)
bot.remove_command('help')
#bot.event
async def on_ready():
await bot.user.edit(nick="New Nickname")
bot.run(TOKEN)
You must have the member object of your bot to change your nickname as nicknames are done in guild's. Then you must edit it.
#bot.event
async def on_ready():
for guild in bot.guilds:
await guild.me.edit(nick="new nickname")

Discord bot that returns guild owner's username discord.py

Hello guys im trying to write a code that gives me the discord server owner but its giving Me 'None'
import discord
client = discord.Client()
TOKEN = 'token'
#client.event
async def on_message(message):
if message.content.find("getowner") != -1:
await message.channel.send(str(message.guild.owner))
client.run(TOKEN)
Can someone please help me with this bot thanks!!
I want to get the discord servers owner by typing getowner in a text channel.
I recommend using it as a command rather than doing on_message, you can do this:
from discord.ext import commands
token = "Token"
client = commands.Bot(command_prefix="!") #Use any prefix
#client.command(pass_context=True)
async def getOwner(ctx):
#await ctx.channel.send(str(ctx.guild.owner.display_name))
await ctx.channel.send(str(ctx.guild.owner))
client.run(TOKEN)
But if you don't want to use a command, you could use regular expression and just keep it as:
import discord
import re
client = discord.Client()
TOKEN = 'token'
#client.event
async def on_message(ctx):
if re.match("getowner", ctx.content):
await ctx.channel.send(str(ctx.guild.owner))
client.run(TOKEN)
EDIT: Also try using intents with your bot code.
import discord
import re
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)
TOKEN = "token"
#client.event
async def on_message(ctx):
if re.match("getowner", ctx.content):
await ctx.channel.send(str(ctx.guild.owner))
client.run(TOKEN)
Same changes can be done with the command version.
It's very simple;
from discord.ext import commands
#client.command(name="getowner")
async def getowner(ctx):
await ctx.send(str(ctx.guild.owner))

discord.py change permission of text channel on on_member_join

I am trying to make a discord bot that gives member on on_member_join read_messages = False. I am only able to achieve this when I iterate through all possible discord text channels in a for loop, but I would only want to do it for a specific channel. I know how to create a channel and give it read_messages = False, but I need to do it on a already existing channel.
My current code:
import time
import discord
from dotenv import load_dotenv
import os
import random
from discord.utils import get
import asyncio
load_dotenv()
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)
#client.event
async def on_member_join(member):
if member.guild.id == 599754972228888888:
print("Correct guild. New member: " + str(member))
else:
return
guild = client.get_guild(599754972228888888)
channel = client.get_channel(517974569018888888)
print(guild.channels)
for cha in channel.channels:
await cha.set_permissions(member, read_messages=False)
client.run(DISCORD_TOKEN)
Is there any reason to not simply deny the #everyone-role reading permission for that specific channel from your Discord server settings?
Assuming channel = client.get_channel(517974569018888888) is the channel you want to adjust the permissions for, you can deny the reading permissions directly doing
await channel.set_permissions(member, read_messages=False)
If channel is not the channel you want or there's some dynamic involved, you can use discord.utils.get to get the channel by providing e.g. the channel name and then proceed with 2.
Neither of the solutions needs the for loop, it can be eliminated entirely
I think you want to make a text channel which is able to see only for administrators or verified user or smth like that.
#client.event
async def on_member_join(member):
channel = discord.utils.get(member.guild.channels, name="NAME")
await channel.set_permissions(member, read_messages=False)
If you want to set perms for everyone do this:
#client.command()
#command.has_permissions(administrator=True)
async def setperms(ctx):
guild = ctx.guild
role = discord.utils.get(guild.roles, name="#everyone")
channel = discord.utils.get(member.guild.channels, name="NAME")
await channel.set_permissions(member, read_messages=False)
Replace NAME with name of your channel

Discord Bot - Give roles after Message, Python

I am trying to make a simple discord bot, that gives a role to a user after he gives the bot a certain command
On the command !role the user should get a role called Beta
I first tried this:
from discord_webhook import DiscordWebhook, DiscordEmbed
import discord
from discord.ext.commands import Bot
from discord.ext import commands
client = commands.Bot(command_prefix = "!")
Client = discord.Client()
#client.event
async def on_message(message):
member = message.author
if member.bot:
return
if message.attachments:
return
print(message.content)
print(str(message.author))
if "role" in message.content:
embed=discord.Embed(title="Giving role.", color=0x00ff40)
await message.channel.send(message.channel, embed=embed)
role = discord.utils.get(server.roles, name="Beta")
await client.add_roles(message.author, role)
client.run("BOT TOKEN")
But I always get the following problem:
AttributeError: 'list' object has no attribute 'roles'
Thanks a lot for taking time to read this and if you can help me. Thanks
When you do:
role = discord.utils.get(server.roles, name="Beta")
You have to use
message.guild.roles
instead of:
server.roles
to access the list of roles, newer discord versions use guild instead of server to avoid confusion with voice servers.

Categories